Link to home
Start Free TrialLog in
Avatar of Zexks
ZexksFlag for United States of America

asked on

To declare array size with datatable property

So I'm trying to declare a 2 dimentional array based off the Rows.Count and Columns.Count properties of a datatable. I keep running into the same issue where the array is never initialized therefor as soon as i attempt to assign anything to it, i get out of bounds.

As shown in the code below if I declare the array's with a simple hardcoded int they work just fine, but for some reason it doesn't like the counts. I have separate detection to check those count values and they appear correct as 1(rows) 8(columns) I just apparently can't instantiate with them. Any ideas?
//Tables filled above 
 
string[,] data1 = new string[Convert.ToInt32(tmpTable1.Rows.Count-1),  Convert.ToInt32(tmpTable1.Columns.Count-1)];
 
string[,] data2 = new string[Convert.ToInt32(tmpTable2.Rows.Count-1), Convert.ToInt32(tmpTable2.Columns.Count-1)];
 
MessageBox.Show(Convert.ToString(data1.Length)); //Returns 0
MessageBox.Show(Convert.ToString(data2.Length)); //Returns 0
MessageBox.Show("row#="+y.ToString()+"\rcolumn#="+x.ToString()); //Returns 1 and 8 respectively
 
 
//assignment loops
foreach(DataRow row in tmpTable1.Rows)
{
	x=1;				
	MessageBox.Show("row#="+y.ToString()+"\rcolumn#="+x.ToString()+"\rforeach row in table1");
 
	foreach(DataColumn col in tmpTable1.Columns)
	{
		MessageBox.Show("row#="+Convert.ToString(y-1)+"\rcolumn#="+Convert.ToString(x-1)+"\rforeachcolumn in table1");
 
		//object cellData1 = row[col];
		data1[y-1, x-1] = row[col].ToString(); //Dies here.
		MessageBox.Show(row[col].ToString()); //Returns Correct Info.
		x++;
	}
	y++;
}

Open in new window

Avatar of Dmitry G
Dmitry G
Flag of New Zealand image

What's x and y?
Why dont you use them when creating an array?
Sorry, I've missed the bottom portion of the snippet...
Inside your loop - what's the initial value of the "y"? May be it's need to be set to 1?
Avatar of Zexks

ASKER

Yea sorry probably should have included that but both x and y are initialized as 1.
ASKER CERTIFIED SOLUTION
Avatar of Zexks
Zexks
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Interesting...

By the way, what's the point for the conversion:

x=Convert.ToInt32(tmpTable1.Columns.Count);

?

I believe Count property already returns integer?
Avatar of Zexks

ASKER

The conversion was just a last ditch effort to get the array to initialize. Then I just copy pasted and just forgot to take it out :P
You're right though it not needed, just a left over. Worst part about all this is that after getting it all to work I came to realize a fatal flaw in the methodology I was using and have since had to scap the whole routine. Sad panda am I :(
I always wonder - what should happen if in the course of a discussion the asker changes his mind. For example - to choose a different approach. Was it a result of the discussion? Or not? Why experts should spend their time to clarify poorly formulated questions? And so on. I don't need these 125 points - just curiosity :)