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

Simple Cloning Question

I've implemented the ICloneable interface on a class of mine. I've
implemented it in two different ways both of which I feel should work
correctly, but only one does and I'd be curious to know why.

Working:

CallflowBase ret = null;
ret = this.MemberwiseClone() as CallflowBase;
ret.m_callflowName = this.m_callflowName;

/ * I'm resetting these values since I want to make sure the user
reintializes the cloned object before using it */
ret.m_initialized = false;
ret.m_deviceID = null;
ret.m_currentNode = null;
ret.m_asyncCommandProcessor = null;
ret.m_syncCommandProcessor = null;

if (this.m_rootNode != null)
{
ret.m_rootNode = this.m_rootNode.Clone() as INode;
}

NOT Working:

/* I figured it would be easier to just create a new instance since I
don't want to copy most of the members */

CallflowBase ret = new CallflowBase();
ret.m_callflowName = this.m_callflowName;

if (this.m_rootNode != null)
{
ret.m_rootNode = this.m_rootNode.Clone() as INode;
}

But it turns out that I'm not actually making a proper copy because I
end up pointing to the same object! Am I missing something obvious
here?

Thanks,
Dan

Sep 21 '07 #1
5 1087
Dan Dorey wrote:
I've implemented the ICloneable interface on a class of mine. I've
implemented it in two different ways both of which I feel should work
correctly, but only one does and I'd be curious to know why.

Working:

CallflowBase ret = null;
ret = this.MemberwiseClone() as CallflowBase;
ret.m_callflowName = this.m_callflowName;

/ * I'm resetting these values since I want to make sure the user
reintializes the cloned object before using it */
ret.m_initialized = false;
ret.m_deviceID = null;
ret.m_currentNode = null;
ret.m_asyncCommandProcessor = null;
ret.m_syncCommandProcessor = null;

if (this.m_rootNode != null)
{
ret.m_rootNode = this.m_rootNode.Clone() as INode;
}

NOT Working:

/* I figured it would be easier to just create a new instance since I
don't want to copy most of the members */

CallflowBase ret = new CallflowBase();
ret.m_callflowName = this.m_callflowName;

if (this.m_rootNode != null)
{
ret.m_rootNode = this.m_rootNode.Clone() as INode;
}

But it turns out that I'm not actually making a proper copy because I
end up pointing to the same object! Am I missing something obvious
here?
Yes, in one call *ret* is assigned to a MermerwiseClone of *this*, in
the other it's just assigned to *this*.

Chris.
Sep 21 '07 #2
Chris,

I don't follow you.

In the example code that works I'm doing:

ret = this.MemberwiseClone() as CallflowBase;

In the example that doesn't work I'm doing:

CallflowBase ret = new CallflowBase();

So I'm not just assigning it to this....

Dan

Sep 21 '07 #3
Dan Dorey wrote:
Chris,

I don't follow you.

In the example code that works I'm doing:

ret = this.MemberwiseClone() as CallflowBase;

In the example that doesn't work I'm doing:

CallflowBase ret = new CallflowBase();

So I'm not just assigning it to this....

Dan
Apologies, I misread the code the first time.

You are only posting a partial code example, and your statements about
what will work and what won't are not in line with anything else I've
seen when working with ICloneable.

Below is a short but concise example that demonstrates both methods work
just fine.

This begs the question though, how are you determining whether or not
you're actually using the same reference?

Chris.
class ClassA : ICloneable
{
public string name;

public ClassA()
{
name = "ClassA";
}

public object Clone()
{
ClassA ret = null;
ret = (ClassA)this.MemberwiseClone();
return ret;
}
}

class ClassB : ICloneable
{
public string name;

public ClassB()
{
name = "ClassB";
}

public object Clone()
{
ClassB ret = new ClassB();
ret.name = this.name;
return ret;
}
}

class General
{
[STAThread]
static void Main(string[] args)
{
ClassA a1 = new ClassA();
a1.name = "The first ClassA";

ClassA a2 = (ClassA)a1.Clone();
a2.name = "The second ClassA";

MessageBox.Show(a1.name + "\n" + a2.name);

ClassB b1 = new ClassB();
b1.name = "The first ClassB";

ClassB b2 = (ClassB)b1.Clone();
b2.name = "The second ClassB";

MessageBox.Show(b1.name + "\n" + b2.name);
}
}
Sep 21 '07 #4
Dan Dorey wrote:
I've implemented the ICloneable interface on a class of mine. I've
implemented it in two different ways both of which I feel should work
correctly, but only one does and I'd be curious to know why.
Please don't post the same message twice. I've already posted a reply
to the other thread you created with the same exact question. Now we
have what is essentially the same thread split into two. It will be
very difficult to correlate various replies from different people.
Sep 21 '07 #5
On Sep 21, 2:42 pm, Peter Duniho <NpOeStPe...@NnOwSlPiAnMk.comwrote:
Dan Dorey wrote:
I've implemented the ICloneable interface on a class of mine. I've
implemented it in two different ways both of which I feel should work
correctly, but only one does and I'd be curious to know why.

Please don't post the same message twice. I've already posted a reply
to the other thread you created with the same exact question. Now we
have what is essentially the same thread split into two. It will be
very difficult to correlate various replies from different people.
Peter. Sorry for posting the same message twice. I posted the original
but after waiting 30 minutes I was still unable to view the post.
Google said that it had been removed or something strange like that. I
assumed that no one else would have been able to read it either.
Apparently that has been resolved now.

As long as the code I posted makes sense, then I'm sure I've done
something stupid outside of the posted code. I'll investigate this
further. Thanks.

Sep 21 '07 #6

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

Similar topics

7
by: sonic | last post by:
Hello, I am cloning a table row which contains images that have behaviors attached to them as well as onclick events. The problem is that the cloned row seems to be executing the...
0
by: meh | last post by:
Greetings all; Got some questions about cloning a treenode.... In this example: private void button4_Click(object sender, System.EventArgs e) { TreeNode lastNode = treeView1.Nodes....
8
by: Tom | last post by:
I've a problem. I want to clone an object having a list of other objects (and so on :/). Do you know any other way than ICloneable.Clone() implementation for all classes in the way? Help..
2
by: Hendrik Schober | last post by:
Hi, I need something like this: class X { private: struct Impl_ { virtual ~Impl_() {} virtual Impl_* clone() const = 0; };
3
by: AVL | last post by:
Hi, I've a query in cloning. How cloning is different from creating a new instance of an object.? I suppose cloning also creates a new object and copies the exisitng object's data. Where and when...
6
by: J Williams | last post by:
I'm using axWebBrowser control and HTML DOM in a VB .NET Windows application to create a new HTML document by cloning nodes. The function below is called from the axWebBrowser1_DocumentComplete...
27
by: karan.shashi | last post by:
Hey all, I was asked this question in an interview recently: Suppose you have the method signature bool MyPairSum(int array, int sum) the array has all unique values (no repeats), your...
3
by: raylopez99 | last post by:
The "C# Cookbook" (O'Reilly / Jay Hilyard), section 3.26, is on deep cloning versus shallow cloning. The scanned pages of this book are found here: http://www.sendspace.com/file/mjyocg (Word...
0
by: Chris | last post by:
Hi All. I'm cloning a treenode, like so: TreeNode trNewTemp = new TreeNode(); trNewTemp = ((((ICloneable)trTempNode).Clone()) as TreeNode); Now the cloning works- trNewTemp has the same...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.