How about basing something on Dictionary<System.Drawing.Point, string>
? So that you can write;
stringGrid.Add(new Point(3,4), "Hello, World!");
Create a class containing such a dictionary and then you get what you're looking for, almost for free. Something like (untested)
class StringGrid
{
Dictionary<System.Drawing.Point, string> grid;
public StringGrid
{
this.grid = new Dictionary<System.Drawing.Point, string>();
}
public string Get(int x, int y)
{
string result = null;
grid.TryGetValue(new Point(x,y), out result);
return result;
}
public void Set(int x, int y, string content)
{
var pos = new Point(x,y);
if (grid.ContainsKey(pos))
{
if (content == null)
{
// no content, so just clear the cell.
grid.remove(pos);
}
else
{
// add new content
grid[pos].Value = content;
}
}
else if (content != null)
{
// new non-null content
grid.add(pos, content);
}
}
}
EDIT: Also, if you want to get really flash;
- replace the dictionary with a
SortedList<,>
- replace
System.Drawing.Point
with your own struct which implements IComparable
This way, the list would be internally ordered by row, then by column, making a simple foreach
loop sufficient to iterate through all the values in the first row, then the values in the second, and so on. Allows you to convert to and `IEnumerable`` -- or a collection of rows.