473,383 Members | 1,863 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,383 software developers and data experts.

How to NOT reference the same object??

Hi All,
Just got started in C#...

Here's some C# code:

public MyClass

{

private MyThingy m_Thingy;

public SetThingy(MyThingy thingy)

{

m_Thingy = thingy;

}

}

I just found out that MyClass.m_Thingy will actually be referencing the same
MyThingy object that the caller passed in. Similar code in C++ would have
invoked the implied = operator and just copied the contents giving me a
separate, independent object with values initialized to what the passed in
object has. This is what I need, but I don't know how to make C# do the
same. How is this accomplished in C#?? I could always write a Copy
Constructor for MyThingy and do m_Thingy = new MyThingy(thingy) but I was
hoping there was another way so I don't have to write the copy constructor
in cases like this.

Thanks for any help,

Wayne


Nov 15 '05 #1
6 1449
WayneD wrote:
Thanks for any help,


You might consider implementing the ICloneable interface in your
MyThingy class. Then you can create a copy like so:

MyThingy newThingy = oldThingy.Clone();

If your class has any member variables that are themselves reference
types, you'll need to make sure you copy those objects as well or
they'll be referenced by both instances.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
Nov 15 '05 #2
You can create a shallow copy this way:

class MyThingy
{
...
public MyThingy Copy()
{
return (MyThingy)this.MemberwiseClone();
}
}

"WayneD" <Wa***@Wayconsoftware.com> wrote in message
news:OQ*************@TK2MSFTNGP10.phx.gbl...
Hi All,
Just got started in C#...

Here's some C# code:

public MyClass

{

private MyThingy m_Thingy;

public SetThingy(MyThingy thingy)

{

m_Thingy = thingy;

}

}

I just found out that MyClass.m_Thingy will actually be referencing the same MyThingy object that the caller passed in. Similar code in C++ would have
invoked the implied = operator and just copied the contents giving me a
separate, independent object with values initialized to what the passed in
object has. This is what I need, but I don't know how to make C# do the
same. How is this accomplished in C#?? I could always write a Copy
Constructor for MyThingy and do m_Thingy = new MyThingy(thingy) but I was
hoping there was another way so I don't have to write the copy constructor
in cases like this.

Thanks for any help,

Wayne

Nov 15 '05 #3
Chris R wrote:
You can create a shallow copy this way:


A shallow copy leads to exactly the same issue: reference types in both
copies point to the same objects.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
Nov 15 '05 #4
A shallow copy creates a new instance of the same type as the original
object, and then copies the nonstatic fields of the original object. If the
field is a value type, a bit-by-bit copy of the field is performed. If the
field is a reference type, the reference is copied but the referred object
is not; therefore, the reference in the original object and the reference in
the clone point to the same object. In contrast, a deep copy of an object
duplicates everything directly or indirectly referenced by the fields in the
object.
A shallow copy leads to exactly the same issue: reference types in both
copies point to the same objects.
He didn't indicate whether the fields would be reference types. I would
consider this "an issue", but "exactly the same issue"; the original
poster's problem was with regard to the host object itself referencing the
same object, and in the case of a shallow clone that is not the situation.

Jon
"Frank Oquendo" <fr*******@acadx.com> wrote in message
news:ep**************@TK2MSFTNGP10.phx.gbl... Chris R wrote:
You can create a shallow copy this way:


A shallow copy leads to exactly the same issue: reference types in both
copies point to the same objects.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)

Nov 15 '05 #5
People have covered the other issues. A quick and cheap way to clone an
entire class' object graph would be to serialize the object and deserialize
it back into an instance. So long as every referred-to object is
serializable, you will get a true deep copy.

If you just want a shallow copy you can do the MemberwiseClone() as
previously suggested, or if value semantics are more appropriate for your
"MyThingy" type, you can define it instead as a struct, which will make a
copy (shallow) on assignment.

Richard

--
Veuillez m'excuser, mon Français est très pauvre. Cependant, si vous voyez
mauvais C #, c'est mon défaut!
"WayneD" <Wa***@Wayconsoftware.com> wrote in message
news:OQ*************@TK2MSFTNGP10.phx.gbl...
Hi All,
Just got started in C#...

Here's some C# code:

public MyClass

{

private MyThingy m_Thingy;

public SetThingy(MyThingy thingy)

{

m_Thingy = thingy;

}

}

I just found out that MyClass.m_Thingy will actually be referencing the same MyThingy object that the caller passed in. Similar code in C++ would have
invoked the implied = operator and just copied the contents giving me a
separate, independent object with values initialized to what the passed in
object has. This is what I need, but I don't know how to make C# do the
same. How is this accomplished in C#?? I could always write a Copy
Constructor for MyThingy and do m_Thingy = new MyThingy(thingy) but I was
hoping there was another way so I don't have to write the copy constructor
in cases like this.

Thanks for any help,

Wayne

Nov 15 '05 #6
Just for the record, a shallow copy using MemberwiseClone will behave
identically to functionality that the default operator= will in C++. A
referenced object in the original object will also be referenced in the
copied object. So, if different behavior is desired, a method would need to
be created for either C# or for C++.

Thus, the original poster's question is answered as far as it went.

Chris R.

"Frank Oquendo" <fr*******@acadx.com> wrote in message
news:ep**************@TK2MSFTNGP10.phx.gbl...
Chris R wrote:
You can create a shallow copy this way:


A shallow copy leads to exactly the same issue: reference types in both
copies point to the same objects.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)

Nov 15 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Jan Pieter Kunst | last post by:
(apologies if this message is a duplicate -- my news server seems to have problems) Greetings, When using PHP 4, this: // ex. 1 class A { function A(&$obj) {
6
by: Chris S. | last post by:
I'm trying to make a graphical editor and browser for Pickled files. One aspect I'm not sure about is how to detect multiple references to the same data. For instance, say I had the Pickled...
4
by: z_learning_tester | last post by:
I'm reading the MS press C# book and there seems to be a contradiction. Please tell me which one is correct, 1 or 2. Thanks! Jeff 1. First it gives the code below saying that it prints 0 then...
3
by: Adam | last post by:
We have a web site that uses .vb for the web pages and .cs for a class module. We are getting the error in .NET 2.0 and VS 2005 beta 2. It does work with .NET 1.1. When trying to access a page...
12
by: Andrew Bullock | last post by:
Hi, I have two classes, A and B, B takes an A as an argument in its constructor: A a1 = new A(); B b = new B(a1);
51
by: Kuku | last post by:
What is the difference between a reference and a pointer?
8
by: toton | last post by:
HI, One more small doubt from today's mail. I have certain function which returns a pointer (sometimes a const pointer from a const member function). And certain member function needs reference...
68
by: Jim Langston | last post by:
I remember there was a thread a while back that was talking about using the return value of a function as a reference where I had thought the reference would become invalidated because it was a...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
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...

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.