By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,458 Members | 1,467 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,458 IT Pros & Developers. It's quick & easy.

Get a copy (not a reference) of a class

P: n/a
I have a class with a custom property called Name = "pepe"

When i create a copy of it (Pepe pepe = oldpepe;)

And change the property of the 'new' class (pepe.Name = "pepe1")
oldpepe.Name has canged to "pepe1"

How can i prevent that oldpepe="pepe" changes?

Nov 15 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a


"~toki" <pedorro77.hotmail.com> wrote in message
news:ui*************@tk2msftngp13.phx.gbl...
I have a class with a custom property called Name = "pepe"

When i create a copy of it (Pepe pepe = oldpepe;)

And change the property of the 'new' class (pepe.Name = "pepe1")
oldpepe.Name has canged to "pepe1"

How can i prevent that oldpepe="pepe" changes?

Hi ~toki,

You can implement the IClonable interface and then call Clone() when you
want a copy.

Joe
--
http://www.csharp-station.com
Nov 15 '05 #2

P: n/a
I would make a copy constructor and a Clone() method

/* can be private or protected */
public Pepe(Pepe p)
{
// Assign this fields here using fields from p
}

public Pepe Clone()
{
return new Pepe(this);
}

HTH
Brian W


"~toki" <pedorro77.hotmail.com> wrote in message
news:ui*************@tk2msftngp13.phx.gbl...
I have a class with a custom property called Name = "pepe"

When i create a copy of it (Pepe pepe = oldpepe;)

And change the property of the 'new' class (pepe.Name = "pepe1")
oldpepe.Name has canged to "pepe1"

How can i prevent that oldpepe="pepe" changes?

Nov 15 '05 #3

P: n/a

"~toki" <pedorro77.hotmail.com> wrote in message
news:ui*************@tk2msftngp13.phx.gbl...
I have a class with a custom property called Name = "pepe"

When i create a copy of it (Pepe pepe = oldpepe;)

And change the property of the 'new' class (pepe.Name = "pepe1")
oldpepe.Name has canged to "pepe1"

How can i prevent that oldpepe="pepe" changes?


Hi,

You're not making a copy of it when you're setting pepe to reference
oldpepe. It's still just one object with two references to it. You should
make the Pepe class implement the ICloneable interface, which means you need
to write something like:

class Pepe : ICloneable
{
public Object Clone()
{
return MemberwiseClone();
}
}

MemberwiseClone is a protected method which all objects inherit from
System.Object.
Now you can write code like:

Pepe pepe = (Pepe) oldpepe;
pepe.Name = "pepe1";
Console.WriteLine(pepe.Name); // Outputs "pepe1"
Console.WriteLine(oldpepe.Name); // Outputs "pepe"

Note that even this only creates a shallow copy of your object. That is, if
you have any references to other objects within Pepe, those objects will be
shared. If you want deep copy, you need to implement that yourself.

Regards,
Einar
Nov 15 '05 #4

P: n/a
Nevermind, don't listen to me.

I forgot about IClonable & MemberwiseClone()

:S


"Brian W" <brianw@gold_death_2_spam_rush.com> wrote in message
news:Oa**************@TK2MSFTNGP10.phx.gbl...
I would make a copy constructor and a Clone() method

/* can be private or protected */
public Pepe(Pepe p)
{
// Assign this fields here using fields from p
}

public Pepe Clone()
{
return new Pepe(this);
}

HTH
Brian W


"~toki" <pedorro77.hotmail.com> wrote in message
news:ui*************@tk2msftngp13.phx.gbl...
I have a class with a custom property called Name = "pepe"

When i create a copy of it (Pepe pepe = oldpepe;)

And change the property of the 'new' class (pepe.Name = "pepe1")
oldpepe.Name has canged to "pepe1"

How can i prevent that oldpepe="pepe" changes?


Nov 15 '05 #5

P: n/a
Thanks to all. It works. :)
I come from Visual Fox where things are differents.
"~toki" <pedorro77.hotmail.com> escribió en el mensaje
news:ui*************@tk2msftngp13.phx.gbl...
I have a class with a custom property called Name = "pepe"

When i create a copy of it (Pepe pepe = oldpepe;)

And change the property of the 'new' class (pepe.Name = "pepe1")
oldpepe.Name has canged to "pepe1"

How can i prevent that oldpepe="pepe" changes?

Nov 15 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.