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

ZexksAsked:
Who is Participating?
 
ZexksAuthor Commented:
Ok so i couldn't exactally figure out why I wasn't able to instantiate those arrays with the Count properties, but I did find a work around for it, so at least it's working now.
				x=Convert.ToInt32(tmpTable1.Columns.Count);
y=Convert.ToInt32(tmpTable1.Rows.Count);				string[,] data1 = new string[y, x];
				
				x=Convert.ToInt32(tmpTable2.Columns.Count);				y= Convert.ToInt32(tmpTable2.Rows.Count);				string[,] data2 = new string[y, x];
 
if(row[col] != null) //To account for the left over row at the end of the table.
{
	data1[y-1, x-1] = row[col].ToString();
	MessageBox.Show(row[col].ToString());
}

Open in new window

0
 
anarki_jimbelCommented:
What's x and y?
Why dont you use them when creating an array?
0
 
anarki_jimbelCommented:
Sorry, I've missed the bottom portion of the snippet...
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
anarki_jimbelCommented:
Inside your loop - what's the initial value of the "y"? May be it's need to be set to 1?
0
 
ZexksAuthor Commented:
Yea sorry probably should have included that but both x and y are initialized as 1.
0
 
anarki_jimbelCommented:
Interesting...

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

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

?

I believe Count property already returns integer?
0
 
ZexksAuthor Commented:
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 :(
0
 
anarki_jimbelCommented:
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 :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.