I have encountered a problem that appears to be a bug in processing jagged arrays in a C# web api program. Everytime I update a single dimension array, it updates a jagged array with the same values even though I don't ask it to. More puzzingly is that through each update iteration of my single dimension array, all of the entries in my jagged array are updated with the same data from the first element up to the current element. It is something I have never encountered before and is very puzzling. Below is my code.
string[][] burialtable = new string[50][]; // the jagged array
string[] mytable = new string[7]; // the single dimension array
int count = 0;
try
{
while (rdr.Read())
{
mytable[0] = rdr.GetString(0);
mytable[1] = rdr.GetString(1);
mytable[2] = rdr.GetString(2);
mytable[3] = rdr.GetString(3);
mytable[4] = rdr.GetString(4);
mytable[5] = rdr.GetString(5);
mytable[6] = rdr.GetString(6);
burialtable[count] = mytable;
count++;
}
rdr.Close();
return burialtable;
After the first iteration through mytable, the first element of burialtable contains the same data as mytable. However, in the debugger I noticed that when I go through the second iteration of mytable, each entry in the first element of burialtable is updated with the values of mytable at the same time as mytable is being populated. On the third iteration of mytable, both the first and second elements of burialtable are updated in concert with mytable as it is being updated. Is this weird or what?
At the end of my processing, the burialtable contains 50 elements of identical data. The identical data came from the last record processed. The output looks like this:
[["Uphoff","William R.","2/20/1985 ","1985","9 ","87 ","4 "],
["Uphoff","William R.","2/20/1985 ","1985","9 ","87 ","4 "],
["Uphoff","William R.","2/20/1985 ","1985","9 ","87 ","4 "],
["Uphoff","William R.","2/20/1985 ","1985","9 ","87 ","4 "],
["Uphoff","William R.","2/20/1985 ","1985","9 ","87 ","4 "],
["Uphoff","William R.","2/20/1985 ","1985","9 ","87 ","4 "],
I have never encountered such a bizarre programming problem as this. It appears that anytime I update a single dimension array I updated all the entries in a jagged array up to the current element even though I don't request it. This is my first time working with jagged arrays so I must be doing something wrong.I am trying to create an array within an array that I can return as a web service in a json format using web api. If someone knows a better way to do this, let me know.
Aucun commentaire:
Enregistrer un commentaire