I have a task which is very simple to script in PHP, which I have worked mostly with for the past several years, but I can't figure out a good way to do it in C#. I hope someone can help.
I am reading x and y coordinates and a corresponding value from a database. This is a mapping of chips on a wafer, and is relatively round, meaning that the x,y pairs which are present will not fill completely a rectangular array.
I do know that the x and y coordinates start at 1 (not zero), but until I have retrieved all the data from the database, I do not know the maximum value for x or for y. And I do not know the total number of rows returned, since the MySQL/Net connector I am using does not seem to be able to provide this.
What I wish to do is to have the data placed into either a rectangular array, or, for more memory efficiency, perhaps a jagged array, such that I can access the values with for example myarray[x,y] and so I can traverse the values using nested foreach loops.
But since I do not know the size of the mapping (total number of rows returned, maximum x, maximum y) I cannot define a dynamic array, or at least I do not know how to. Also, since neither x nor y cannot take on the value of 0, my arrays should be indexed to start at 1, not 0.
Can anyone tell me how this best could be done? I came up with a way today which to me does not seem too efficient: I retrieve the data and put them into a hashtable of hashtables, and calculate the maximum x and y while doing this. I then create a rectangular array of size [max x, max y] filled initially with zeros, and then fill this array with the values from the hashtables. This works, but it just does not seem very efficient to me.
Thanks for any help!
stevePosted Jul 4, 2008, 2:57 AM
Thank you for the reply! In the meantime I actually came to a similar solution as you suggest.
I defined a struct with members x, y and value.
I then populated an array list with these structs as I read them from the database. While doing this I find out the maximum x and maximum y.
I then make a rectangular array using these maximum x and y values as dimensions.
I then populate the rectangular array from the values stored in the array list of structs. I do this step instead because later use of the data is far easier if it is in the form of a 2D array with keys x and y.
This seems to work, and is a lot cleaner than my original idea with hashtables.
Guest UserPosted Jul 3, 2008, 3:00 PM
Then populate an ArrayList with instances of this new object.