9

I have some strange problem where all my string arrays has the same value in the List. Here is my code:

List<string[]> map_data = new List<string[]>();
string[] map_data_array = new string[11];

for(int i = 0; i < 2000; i++)
{
    map_data_array = PopulateDataFromFile(); // it returns different data every call
    map_data.Add(map_data_array); // store to List
}

map_data_array has always different data, I've verified that by placing the break point there and I've checked it.

The problem is that map_data has the value of all elements the same. And this value is the data that comes from function PopulateDataFromFile when the i is 1999.

What I am doing wrong? :/

7
  • 9
    post the code for PopulateDataFromFile() as well Commented Jun 18, 2013 at 11:01
  • in the immediate window try this map_data[0] == map_data[1999] if it returns true you are adding the same array 2000 times (that is PopulateDataFromFile() returns the same array if it returns false then PopulateDataFromFile() returns a new array every time but with the4 same content Commented Jun 18, 2013 at 11:04
  • @Golgauth please don't add tags to title, it was removed for a purpose. Also don't use inline code to mark every keyword in the post it's creating too much "noise". Commented Jun 18, 2013 at 11:06
  • Check one more thing while you debug - in the second iteration when you see map_data_array has returned different values, check that map_data[0] would also have the same value. That means somewhere your code is updating same reference. Commented Jun 18, 2013 at 11:07
  • @ShadowWizard Ok, noted for the inline code, I will never do this again, I promise ;-) But I never changed the title... Commented Jun 18, 2013 at 11:08

4 Answers 4

12

That only happens if you place the same array into the list. As you did not give the code to PopulateDataFromFile we can only guess what happens. Make sure that the function returns a seperate array created with new each time.

Sign up to request clarification or add additional context in comments.

10 Comments

Thank you, I got declared "map_data_array" on the "global" scope.
But why this happens? Do the list actually "link" the object and not save/copy the data that this object has?
@tilenslo This is happening because PopulateDataFromFile(); is probably returning the same data on each iteration.
Yep! That's a good synthesis, regarding the few intell we have here.
@svick Hmmm ` So, if you look at the array PopulateDataFromFile() returned right after it returns, it looks like it's returning different data each time.` why am I having a hard time believing this? Since we don't have concrete code for the method we can only speculate as to its implementation.
|
1

You need to process your data in chunks since PopulateDataFromFile(); looks to be returning all of its data in one go (or as much as the array can fit). Using an extension method, you could do something like this: -

List<string[]> map_data = new List<string[]>();
foreach (var batch in PopulateDataFromFile().Batch(11))
{
       map_data.Add((batch.ToArray());
}

Extension method: -

public static IEnumerable<IEnumerable<T>> Batch<T>(this IEnumerable<T> items, int batchSize)
{
     return items.Select((item, inx) => new { item, inx })
                 .GroupBy(x => x.inx / batchSize)
                 .Select(g => g.Select(x => x.item));
}

6 Comments

I really don't see how is processing in chunks going to help. And why 11? Why not 10000000?
Probably has something to do with: string[] map_data_array = new string[11];... Looks to me like PopulateDataFromFile(); could be returning everything, feel free to disagree though :)
Right, I didn't realize that. But that means your code makes even less sense, since it's always going to return a single batch.
??? How so? Consider a collection size of 2000. On what planet would it return a single batch?
I assumed PopulateDataFromFile() always returns a 11-element array. But in any case, i still don't see any situation where batching the data could fix the issue.
|
0

PopulateDataFromFile() is returning a String array with the same values.

1 Comment

The question says differently: “map_data_array has always different data”.
-5

In the loop everytime you just change the address of map_data_array , so that's why always the value will get changed to the newer data obtained from the method call. Reinitialize the string array everytime will help. It should look something like this

    for(int i = 0; i < 2000; i++)
    {
         string[] map_data_array = PopulateDataFromFile(); // it returns different data every call
         map_data.Add(map_data_array); // store to List
    } 

or if its confusing for you can you make it simple by

    for(int i = 0; i < 2000; i++)
    {             
         map_data.Add(PopulateDataFromFile()); // store to List
    } 

5 Comments

i don't understand the answers here... it's essentially the same as what the OP does.
Sorry, that's wrong. Only the return value of the function decides what gets stored in the list, no matter what variable you store it in temporarily (as long as you don't make copies).
Are you saying that if you declare map_data_array outside the loop and then change it, the value in the list will also change? That's certainly not true.
That doesn't change anything. The declaration does not determine the storage location, only new does.
yes understood , you have to make sure that the return value from the method (PopulateDataFromFile) is always a new object instead of just changing the existing object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.