Jump to content
  • Advertisement
Sign in to follow this  
thk123

[.net] The Mysterious Empty Array

This topic is 3618 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello there, I was wondering if anyone could help me with my mysterious bug. The code is written in XNA, however, the part that I need help with is purely C# so I believe this is the forum to post it in to. Please move and accept my apologies if it isn't. The problem is, when I return an array in one of my methods, despite having put data in to it a line a go, it comes out blank. When I step through the code, the data does go in correctly. Also, I have created a custom class called sArray (super Array), basically, it is an array which has a bunch of extra functions which I have always found annoying that arrays don't have automatically. One of them is called blank. What it does is replaces all the data with "" (or something else of your choosing) A bit of code is now required to make things a little clearer.
[source lang = "c#"]
public string[] msgReturn()
        {
            string[] data = new string[messages.content.Length];
//When using the line by line debug feature, the data array contains the content of messages.content (a string[])
            data = messages.content;
            messages.clear();
            return data;
            
        }

The clear function is as follows:
[source lang = "c#"]
public void clear()
        {
            for(int i = 0; i<content.Length; i++)
            {
                content = "";
            }
        }

I can't work out why data is being blanked before being returned. Any help would be greatly appreciated.

Share this post


Link to post
Share on other sites
Advertisement
It's pretty obvious, once you remember that C# uses reference semantics. Here's what happens:

// You create an array, and then make the variable 'data' reference that new array
string[] data = new string[messages.content.Length];

// You make the variable 'data' reference the array 'messages.content'. The array formerly
// referenced by 'data' is now floating without references and will be reclaimed by
// the GC on the next sweep.
data = messages.content;

// I assume this clears the data in 'messages.content'.
messages.clear();

// You return the reference to 'messages.content' (which has been emptied in the mean time).
return data;


What you want to do is create a copy of the array (which '=' does not do, since it assigns a reference). This would be done using Array.Clone.

Share this post


Link to post
Share on other sites
simple guess: it doesn't make a copy, it takes a reference. and this references to the original messages which you clear => your data is empty, too

why do you clear it, just return the reference and let the gc handle the cleanup laters.

Share this post


Link to post
Share on other sites
Thanks for the replies, that is correct. I thought it might be something like that but I didn't realise how C# handled references. Thanks for the help.

Share this post


Link to post
Share on other sites
This is partially off-topic, but could be useful for you:

If you are using .NET 3.5(Visual Studio 2008), you could just write a bunch of extension methods for the Array class with all those snazzy features you want. That way, you won't have to overload the array class yourself or write your own function that wraps things for you.

For instance, if you want to write a function to pre-initialize the array with a specific value, you could do this:


public static class ArrayExtensions
{
public static void ArraySet(this Array arr, Object initialValue)
{
for (int count = 0; count < arr.Length; count++)
arr.SetValue(initialValue, count);
}
}

String[] myDemoArray = new String[10];
myDemoArray.ArraySet(new String("LOL"));



Toolmaker

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!