Click here to Skip to main content
15,920,217 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all,

How to assign array values to 2d array?



C#
string[,] two_array;

        for (int i = 0; i < SSDBR.ShpServc.Length ; i++)
        {
            two_array={ {SSDBR.ShpServc[i]}};
        }


thanks in advance.
Posted

Hi Try like this,
this might help you..


C#
int length = SSDBR.ShpServc.Length;

            string[,] two_array = new string[1, length];

            for (int i = 0; i < length; i++)
            {
                two_array[0, i] = SSDBR.ShpServc[i];
            }
 
Share this answer
 
Comments
Bojjaiah 20-Dec-13 3:57am    
thank you karthik :) 5+
Karthik_Mahalingam 20-Dec-13 4:37am    
Thanks Bojjiaiah :)
That isn't going to work: you will have to define the size of the whole two_array in advance and then copy the individual entries:
C#
string[,] two_array = new string[SSDBR.ShpServc.Length,SSDBR.ShpServc.Width];
for (int i = 0; i < SSDBR.ShpServc.Length; i++)
    {
    for (int j = 0; j < SSDBR.ShpServc.Width; j++)
        {
        two_array[i, j] = SSDBR.ShpServc[i][j];
        }
    }
The other solution is to use a jagged array:
C#
string[][] two_array = new string[SSDBR.ShpServc.Length][];
In which case you can do this:
C#
for (int i = 0; i < SSDBR.ShpServc.Length; i++)
    {
    two_array[i] = SSDBR.ShpServc[i];
    }
 
Share this answer
 
Comments
Bojjaiah 20-Dec-13 3:57am    
thank you OriginalGriff :) 5+
OriginalGriff 20-Dec-13 4:14am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900