Sunday, 8 September 2013

Iterate Dictionary Key-Value to Populate jagged arrays

Iterate Dictionary Key-Value to Populate jagged arrays

I want to convert the code below
string[][] mergeData = new string[][] {
new string[] { "phone", "date"},
new string[] { "0421 3359 129",
DateTime.Now.ToString() } };
to
Dictionary<string, string> list = new Dictionary<string, string>();
list.Add("phone", "0421 3359 129");
list.Add("date", DateTime.Now.ToString());
string[][] mergeData = new string[2][];
int i = 0;
foreach(KeyValuePair<string, string> pair in list)
{
mergeData[0][i] = pair.Key;
mergeData[1][i] = pair.Value;
i++;
}
But i got error message
object reference not set to an instance of an object
I suspect it has something to do with my jagged array init. I don't know
how to properly initiate jagged array. And I must used jagged array.
Please help. Thanks.

No comments:

Post a Comment