Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>How about basing something on <code>Dictionary&lt;System.Drawing.Point, string&gt;</code>? So that you can write;</p> <pre><code>stringGrid.Add(new Point(3,4), "Hello, World!"); </code></pre> <p>Create a class containing such a dictionary and then you get what you're looking for, almost for free. Something like (untested)</p> <pre><code>class StringGrid { Dictionary&lt;System.Drawing.Point, string&gt; grid; public StringGrid { this.grid = new Dictionary&lt;System.Drawing.Point, string&gt;(); } 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); } } } </code></pre> <p>EDIT: Also, if you want to get really flash;</p> <ul> <li>replace the dictionary with a <code>SortedList&lt;,&gt;</code></li> <li>replace <code>System.Drawing.Point</code> with your own struct which implements <code>IComparable</code></li> </ul> <p>This way, the list would be internally ordered by row, then by column, making a simple <code>foreach</code> 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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload