Reputation: 692
i have a dynamic List like this :
dynamic[,] Tablevalues = {
{ 1 ,"FirstName" , 11111111111111 },
{ 2 ,"SecondName" , 22222222222222 },
{ 3 ,"ThirdName" , 33333333333333 }
};
and i want to loop it so i can add it later in my .Mdf DataBase , and what i tried so far is :
for (int i=0; i<Tablevalues.Length; i++){
// ----- output just for test ------
MessageBox.Show( Tablevalues[i, i].ToString() );
}
but the result of "Tablevalues[i, i]
" only show me :
1 , ,
,SecondName ,
, ,33333333333333
Upvotes: 0
Views: 104
Reputation: 1591
Because you need two for
for 2d array:
for (int i = 0; i < Tablevalues.GetLength(0); i++)
{
for (int j = 0; j < Tablevalues.GetLength(1); j++)
{
MessageBox.Show(Tablevalues[i, j].ToString());
}
}
Upvotes: 1
Reputation: 14967
for (int i = 0; i < Tablevalues.Length; i++) {
MessageBox.Show(Tablevalues[i, i].ToString());
}
You only have one loop variable, so you're only looping from 0, 0
, 1, 1
... up to Length - 1, Length - 1
(i.e. the diagonal.)
Use two for
-loops, along with GetLength(dimension)
to get the rows/columns.
Something like:
for (int i = 0; i < Tablevalues.GetLength(0); i++) {
for (int j = 0; j < Tablevalues.GetLength(1); j++) {
MessageBox.Show(Tablevalues[i, j].ToString());
}
}
Upvotes: 2