My name is Edward Tanguay. I'm an American software and web developer living and working in Berlin, Germany.
C# CODE EXAMPLE created on Sunday, August 22, 2010 permalink
How to use a Dictionary<> with struct key to save a dynamic matrix of objects
The following is an example of how you can save both a fixed matrix of objects and a dynamic matrix of objects (size determined at runtime) using first a multi-dimensional array and then a generic dictionary with a struct key. In both examples you can pick the object out of the map with the x/y coordinates.
using System;
using System.Windows;
using System.Windows.Controls;
using System.Collections.Generic;

namespace TestDoubarray
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();

            StaticMatrixExampleWithArray();
            DynamicMatrixExampleWithDictionary(10, 10);
            DynamicMatrixExampleWithDictionary(20, 20);
        }

        private void StaticMatrixExampleWithArray()
        {

            CheckBox[,] checkBoxes = new CheckBox[10, 10];

            for (int x = 0; x < 10; x++)
            {
                for (int y = 0; y < 10; y++)
                {
                    CheckBox cb = new CheckBox();
                    cb.Tag = String.Format("x={0}/y={1}", x, y);
                    checkBoxes[x,y] = cb;
                }
            }

            CheckBox cbOut = checkBoxes[4, 8];
            Message.Text += cbOut.Tag.ToString() + Environment.NewLine;
        }

        private void DynamicMatrixExampleWithDictionary(int xMax, int yMax)
        {
            Dictionary<MatrixCoordinates, CheckBox> checkboxes = new Dictionary<MatrixCoordinates, CheckBox>();

            for (int x = 0; x < xMax; x++)
            {
                for (int y = 0; y < yMax; y++)
                {
                    CheckBox cb = new CheckBox();
                    cb.Tag = String.Format("x={0}/y={1}", x, y);
                    checkboxes.Add(new MatrixCoordinates { X = x, Y = y }, cb);
                }
            }

            CheckBox cbOut = checkboxes[new MatrixCoordinates { X = 4, Y = 8 }];
            Message.Text += cbOut.Tag.ToString() + Environment.NewLine;
        }

        private struct MatrixCoordinates
        {
            public int X { get; set; }
            public int Y { get; set; }
        }
    }
}
need markup?