473,386 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Need C# help: how do I do this (arrays)?

310 100+
I have data in a MySQL database that I need to pull out into C# arrays (or hashes or whatever) and I cannot find the best way to do it, and also have some problems with syntax.

The data represents a 2 dimensional mapping of chips on a relatively round wafer, and each row of the returned data will contain the following:

x coordinate, y coordinate, and the chip type at this x-y position.

When I do this in PHP, I simply create a two dimensional array on the fly:

mydata[x coordinate][y coordinate] = chip type.

Because of the rounded shape of the mapping, the result would not be a completely filled rectangular 2 dimensional array. But this is only a minor point as long as I can fill the "empty" locations with a NULL chiptype.

I do not know before getting the data out of the database what the overall dimensions of the mapping will be (maximum x and maximum y values).

How can I do this in C#? The MySql connector does not give the total number of rows returned from the select query, I would need to increment a counter as I read the data row by row.

I would like the data to be in the form of a two-dimensional array, either rectangular or jagged, or in any other container that gives me the ability to navigate through the mapping by looping over x and y in the fashion of one foreach loop embedded in another.

Thanks for any help!
Jul 2 '08 #1
4 1294
cloud255
427 Expert 256MB
I
x coordinate, y coordinate, and the chip type at this x-y position.
this sounds like one object with 3 fields: x, y and a 'chip'

When I do this in PHP, I simply create a two dimensional array on the fly:

mydata[x coordinate][y coordinate] = chip type.
this sounds like a chip object consists of a xy co-ordinate pair.

maybe i'm just not reading it right but could you be more clear on this point?
Jul 2 '08 #2
coolsti
310 100+
I guess you can say it is an object with three fields, an x coordinate, a y coodinate, and a chip type.

To be more clear, let us say I represent the chip type by an integer, then some data may look like this, each line showing x : y : chiptype

1 : 34 : 9
1 : 35 : 9
1 : 36 : 8
2 : 31 : 8
2 : 32 : 9

etc.

The point is, I need the data retrieved and placed in a container that allows me to look up any individual value as you normally would do in a two dimensional array, for example mydata[2][31] will give me the value 8 using the above data.

I also need the data in a fashion that I can traverse the entire mapping row by row, as here if this were legal in this language:

Expand|Select|Wrap|Line Numbers
  1. foreach ( "loop over the x coordinates" ) {
  2.    foreach ( "loop over the y coordinates") {
  3.        .... loop code ....
  4.    }
  5. }
  6.  
This is very easy to do in PHP, but that is an interpretive script language.

My question is now to (best) do it in C#?
Jul 2 '08 #3
cloud255
427 Expert 256MB
dont know if this is the best way,

i would use ArrayLists. these allow you to create dynamic size arrays at runtime. this class accepts argumetns of type OBJECT so can put an integer in without any problems, you'll just have to cast back to an int when reading out of the array list.

this method creates one single dimension arrayList inside another one. there is a problem in that C# doesnt realize that you are making a pseudo 2D array so some additional casting is involved.

you will need to include "using System.Collections;"

assing data:

Expand|Select|Wrap|Line Numbers
  1. ArrayList x, y = new ArrayList();
  2.  
  3. y.Add(yourChipValue);
  4.  
  5. x.Add(y);
access data:

Expand|Select|Wrap|Line Numbers
  1. int myData = Convert.ToInt32(((ArrayList)x[i])[k]);
i think this will work.

good luck
Jul 2 '08 #4
coolsti
310 100+
Thanks! I will give it a try.

I also have tried something perhaps not very efficient when you think about it:

Expand|Select|Wrap|Line Numbers
  1. int idchip;
  2. string chipname;
  3. int xpos, ypos;
  4. int curx = 0;
  5. Hashtable layoutrows = new Hashtable();
  6. Hashtable cols = new Hashtable();
  7.  
  8. int i = 0;
  9. int xmax = 0;
  10. int ymax = 0;
  11.  
  12. // Here, already have performed the query with MySqlDataReader reader. 
  13. // Using order by clauses, the data is ordered by the x and y coordinates 
  14. // in ascending order. Now retrieve the data row by row.
  15. while (reader.Read() != false)
  16. {
  17.                 // get data and fill arrays
  18.                 idchip = reader.GetInt32(0);
  19.                 xpos = reader.GetInt32(2);
  20.                 ypos = reader.GetInt32(3);
  21.  
  22.                 // find the maximum x and y coordinates in this mapping
  23.                 xmax = (xmax < xpos) ? xpos : xmax;
  24.                 ymax = (ymax < ypos) ? ypos : ymax;
  25.                 if (xpos != curx)
  26.                 {
  27.                     // A new x coordinate is found, meaning a new row in the
  28.                     // mapping. Copy the hashtable built up so far into the
  29.                     // current "mapping row" hashtableand start with a new hashtable
  30.                     // for the next mapping row.
  31.                     if (curx > 0)
  32.                     {
  33.                         //Note: first coordinate values in the database are 1, not 0, so
  34.                         // do not do this if the curx counter is equal to 0
  35.                         layoutrows[xpos] = cols.Clone();
  36.                         cols = new Hashtable();
  37.                     }
  38.                     cols[ypos] = idchip;
  39.                     curx = xpos;
  40.                 }
  41.                 else
  42.                 {
  43.                     cols[ypos] = idchip;
  44.                 }
  45.                 i++;
  46.             }
  47.             reader.Close();
  48.  
  49.             // save the number of chips for later use
  50.             numchips = i;
  51.  
  52.             // Now get all the data out of the hashtables and put into a
  53.            //  rectangular array.
  54.             int[,] layoutarray2 = new int[xmax+1,ymax+1];
  55.             foreach (int x in layoutrows.Keys)
  56.             {
  57.                 Hashtable hs2 = (Hashtable)layoutrows[x];
  58.                 foreach (int y in hs2.Keys)
  59.                 {
  60.                     layoutarray2[x,y] = (int)hs2[y];
  61.                 }
  62.             }
  63.  
In the above, I decided it may be far easier to work with the data later, if it was in the form of a rectangular array. If a coordinate x,y pair is not in the database, then the chiptype gets set to zero.

The inefficiency here, is that I first pull out all the data as hashtables, and then take the data from the hashtables to place it in the rectangular array.

The reason I cannot put the data into a rectangular array initially is that I do not know before I pull out all the data what the values for xmax and ymax will be. I could get this with an additional database query, but I would rather not.

Would maybe getting the data initially into an ArrayList be better than the hashtables like I am doing here?
Jul 2 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Dariusz | last post by:
I want to use arrays in my website (flat file for a guestbook), but despite having read through countless online tutorials on the topic, I just can't get my code to work. I know there are...
41
by: Psykarrd | last post by:
I am trying to declare a string variable as an array of char's. the code looks like this. char name; then when i try to use the variable it dosn't work, however i am not sure you can use it...
2
by: Pasacco | last post by:
dear I want to ask help on this problem. Array a is partitioned into a0 and a1 in main(). Then a1 is partitioned into a2 and a3 in th_partition() function. And I think this problem is something...
1
by: John Smith | last post by:
I have a two dimentional char array. Before filling it using strtok(), I reset its elements to '\0' using two nested for loops. The code works as I hope it would but I wonder whether I really need...
2
by: Thomas Connolly | last post by:
Anyone know if there is a C# equivallent to: enum { LIFFE_SIZE_AUTOMARKETREF = 15 }; typedef char LiffeAutoMarketReference ; Thanks,
8
by: hothead098 | last post by:
ASSIGNMENT (4) USING AND MANIPUPATING ARRAYS (Chapter 10 material) For this assignment you are to: 1) Create and manage arrays a) One of type integers (containing 10 elements). b) One of...
23
by: vinod.bhavnani | last post by:
Hello all, I need desperate help Here is the problem: My problem today is with multidimensional arrays. Lets say i have an array A this is a 4 dimensional static array.
1
by: rllioacvuher | last post by:
I need help with a program. I have implemented that following header file with an unordered list using one array, but i need to be able to use an ordered list and 2 arrays (one for the links and one...
7
by: thegreatest21 | last post by:
Right, I am making a Tax Calculator and need an array to store the data that has been typed in when the user is prompted. However, being completely new to Java I am having some difficulty getting to...
0
by: sumalats | last post by:
Hello, I need to use Visual basic to acquire some data through the serial port, store it etc. As I am just getting acquainted with VB6 I need some help. My Controller board (8 bit micro) sends...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.