Jump to content
  • Advertisement
Sign in to follow this  
Calin

[.net] Copying an object to another [solved]

This topic is 4439 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

class Something
{
private List <string> Strings;
public void clear()
{
Strings.Clear();
}
}

Something A;
Something B;

A = B;
A.Clear();



Will A.Clear() make the B.Strings empty too? It shouldn't in my opinion. However this is what takes place in my code. If I call A.Clear() ,B.Strings gets emptied, otherwise it stays intact. [Edited by - Calin on April 24, 2006 1:25:12 PM]

Share this post


Link to post
Share on other sites
Advertisement
Thats what .Clone is for. To use it your object needs to implement IClonable. I'd be more specific, but I'm a VB.Net guy and you're clearly not.

Share this post


Link to post
Share on other sites
Quote:
Original post by Calin
*** Source Snippet Removed ***
Will A.Clear() make the B.Strings empty too? It shouldn't in my opinion.

Why shouldn't it? You're not copying B to A, you're making A a reference to B. Unfortunately you can't overload the = operator in C#. You could create a Copy method though:


class Something
{
private List<string> Strings;

public void clear()
{
Strings.Clear();
}

public string GetStringItem(int index)
{
return Strings[index];
}

public void SetStringItem(int index, string data)
{
Strings[index] = data;
}

public Something Copy(Something obj)
{
for(int i=0;i<Strings.Count;i++)
obj.SetStringItem(i,Strings);
}


Share this post


Link to post
Share on other sites
Thanks!

I knew you can't use pointers in C# so I thought the refereces don't work either.

Making a Clone function made it. I'll take a look into IClonable too. I'm still newbie to C#.

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!