After a day of troubleshooting I've managed to condense the problem to this tiny piece of code. Could someone explain to me why this doesn't work? I expect [markets] to be 0 2 4 6, [city] [county] and [streets] to be 0 1 2 3 when the messagebox is shown.
private void pieceoftestcode()
{
string[] county = new string[4];
string[] city = new string[4];
string[] markets = new string[4];
string[] streets = new string[4];
string[] items = new string[4] { "apple", "banana", "pineapple", "juice" };
string[] value = new string[4];
foreach (string item in items)
{
for (int i = 0; i <= 3; i++)
{
if (item == "apple")
value[i] = (2 * i).ToString();
else
value[i] = i.ToString();
}
if (item == "apple")
markets = value;
else if (item == "banana")
streets = value;
else if (item == "pineapple")
county = value;
else
city = value;
}
MessageBox.Show("test");
}
I'm looping through items in a foreach loop. If the item is "apple", then I expect [value] to be 0 2 4 6. Initially [markets] is assigned 0 2 4 6. However, if I execute the code step by step, it appears that the second time that the foreachloop is executed, [markets] is being overwritten. Why is that? What am I doing wrong here? [markets] should not be assigned a value a second time once banana has hit right?
valuethroughout this, that you keep overwriting and gradually assigning references from other variables to this same array.markets = valuesets both references to the same array - and since you're assigning different values to thevaluesarray, naturally you can see these changes reflect regardless of what reference you're using to the array.