D
Dan Dorey
I've implemented the ICloneable interface on one of my class. I've
written this simple code in two different ways and I think both should
work but it's not the case and I'm curious to understand why.
The working method:
CallflowBase ret = null;
ret = this.MemberwiseClone() as CallflowBase;
ret.m_callflowName = this.m_callflowName;
/* I'm resetting some of the members (to be safe) since the cloned
copy will have to be reinitialized before being used. */
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;
}
The NON working method:
/* I thought it would be simpler to just new up a new class in this
case as I don't really 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 appears that this method of cloning fails since I end up
pointing to the same object. Am I missing something obvious here?
Thanks,
Dan
written this simple code in two different ways and I think both should
work but it's not the case and I'm curious to understand why.
The working method:
CallflowBase ret = null;
ret = this.MemberwiseClone() as CallflowBase;
ret.m_callflowName = this.m_callflowName;
/* I'm resetting some of the members (to be safe) since the cloned
copy will have to be reinitialized before being used. */
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;
}
The NON working method:
/* I thought it would be simpler to just new up a new class in this
case as I don't really 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 appears that this method of cloning fails since I end up
pointing to the same object. Am I missing something obvious here?
Thanks,
Dan
