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
Chris Shepherd - 21 Sep 2007 16:58 GMT
> 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
[quoted text clipped - 35 lines]
> 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.
Dan Dorey - 21 Sep 2007 18:15 GMT
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
Chris Shepherd - 21 Sep 2007 19:28 GMT
> Chris,
>
[quoted text clipped - 11 lines]
>
> 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);
}
}
Peter Duniho - 21 Sep 2007 19:42 GMT
> 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.
Dan Dorey - 21 Sep 2007 20:17 GMT
> > 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
[quoted text clipped - 4 lines]
> 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.