Hi all,
I just try to input integer typed table rows into 2D integer array. I've tried the code below but it outputs me only last row :(

Input Table :
-----------------------
id    red  green  blue
1101  12   11     13
1102  12   12     14  
1103  13   14     13   
------------------------
2D Array :   array<Int32,2> ^y_k = gcnew array<Int32,2>(5,4);
// INPUT TABLE ROWS INTO 2D ARRAY 
 while (reader->Read())  
	{
	//for(int i=1; i<2; i++)    
	for(int i=1; i<4; i++)   
	 {    
	y_k[i,1] = Convert::ToInt32( reader["id"] ); 
	y_k[i,2] = Convert::ToInt32( reader["red"] ); 
	y_k[i,3] = Convert::ToInt32( reader["gren"] );  
        y_k[i,4] = Convert::ToInt32( reader["blue"] );  
	 } 
						   
	 }
// CHECKING ARRAY INPUT    
 for(int j=1; j<4; j++) //column   
	{
	for(int i=1; i<5; i++)  // row 
	{     					      textBox1->AppendText(Convert::ToString(y_k[i,j])) ; 
textBox1->AppendText(" ");
	} 
	textBox1->AppendText("||");

	 }

Recommended Answers

All 5 Replies

Why do you have that extra loop in the process?

//UNTESTED, but should look something like this:
int i=0;
while (reader->Read())  
{
   y_k[i,1] = Convert::ToInt32(reader["id"]->ToString()); 
   y_k[i,2] = Convert::ToInt32(reader["red"]->ToString()); 
   y_k[i,3] = Convert::ToInt32(reader["gren"]->ToString());  
   y_k[i,4] = Convert::ToInt32(reader["blue"]->ToString());  
   i++;
}

Thines01, Thank you 1111111 times .
Now the array outputs all rows . What was the logic, why did you think like that??

You were re-inserting all of the rows (with the inner loop) over top of the elements in the array.
It was actually looping 16 times to put data in the array, when it only needed to loop 4 (given 4 rows).

Sorry thines01, i couldn't see your post. So sorry.
First, could you say how many loops are there (in the code below) :

while (reader->Read()) // READS 12 ITEMS
for(int i=1; i<4; i++) // (loops 3*4 times)
{
y_k[i,1] = Convert::ToInt32( reader["id"] );
y_k[i,2] = Convert::ToInt32( reader["red"] );
y_k[i,3] = Convert::ToInt32( reader["gren"] );
y_k[i,4] = Convert::ToInt32( reader["blue"] );
} 
}

Finally, could you tell me your idea about the (if any) performance difference btw the following codes ?!

while (reader->Read())
for(int i=1; i<4; i++)
{
y_k[i,1] = Convert::ToInt32( reader["id"] );
y_k[i,2] = Convert::ToInt32( reader["red"] );
y_k[i,3] = Convert::ToInt32( reader["gren"] );
y_k[i,4] = Convert::ToInt32( reader["blue"] );
} 
}
int i=0;
while (reader->Read())
{
y_k[i,1] = Convert::ToInt32(reader["id"]->ToString());
y_k[i,2] = Convert::ToInt32(reader["red"]->ToString());
y_k[i,3] = Convert::ToInt32(reader["gren"]->ToString());
y_k[i,4] = Convert::ToInt32(reader["blue"]->ToString());
i++;

}

One (the top one) will fail as it keeps overwriting the values in the 2d array.
The array will be completely filled with the last record in the db four times.

It's really not a performance issue, but in this case, the bottom one will be faster bacause it loops 4 times instead of 16.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.