473,385 Members | 2,044 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Get a copy (not a reference) of a class

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
5 3418


"~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
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

"~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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
14
by: MSR | last post by:
I have a couple of questions. 1. Copy Constructor. class A { private: int a1; double d1; char *ptr;
5
by: lion | last post by:
in .net, if you set annstance-A of a class equal to another instance-B, a pointer will add to B, but if i want to create a copy of B instead of pointer, how to operate? Note:serialization...
6
by: Jaro | last post by:
hi there, I've problem to create copy of collection of classes. ex.: d1 as mycollection, d2 as mycollection. when I simple set d1=d2 then d1 contain all classes from d2 but their properties are...
2
by: Martin Ortiz | last post by:
Ugh.... All classes are copy by reference, even if you use "ByVal" and NOT "ByRef" it's still a copy by reference. Of course, as a consequence, if you change any values of the object you passed...
2
by: Jeremy Smith | last post by:
I am writing a program where speed is crucial, and one optimisation is that blocks of memory are re-used instead of being copied around and re- allocate. I have written my own vector-style class...
1
by: blangela | last post by:
3.0 Advanced Topic Addendum There are a few cases where the C++ compiler cannot provide an overloaded assignment operator for your class. If your class contains a const member or/and a...
22
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float...
3
by: groups2 | last post by:
Can someone please explain the results below We switched to PHP 5 if I make a copy of an object, and then change the variable inside the object the change is reflected in the copy. Either the...
6
by: Peng Yu | last post by:
Hi, I'm wondering if the following assignment is lazy copy or not? Thanks, Peng std::vector<intv. v.push_back(1); v.push_back(2);
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.