To Clone or Not To Clone?

G

Guest

Hi there,
Consider this from MSDN
*Notes to Inheritors When you derive from DataGridViewCheckBoxCell and add
new properties to the derived class, be sure to override the Clone method to
copy the new properties during cloning operations. You should also call the
base class's Clone method so that the properties of the base class are copied
to the new cell. *

How should I do this?
a)
public override object Clone()
{
MemberwiseClone();
}
// this is no good jsut a shallow copy
b)
public override object Clone()
{
NumericDataGridViewTextBox tb = (NumericDataGridViewTextBox)base.Clone();
tb.MaxValue = this.MaxValue;
tb.MinValue = this.MinValue;
return tb;
}

// This seems to work; but is it? I.e. the base.Clone is only returning a
base object so surely when i cast to a derived object and start writing my
max min integers i over write some of the base somehow?

....tnx in advance, brian
 
B

Bruce Wood

It really depends upon how smart the base class's Clone is. If it
simply instantiates a base object, then your cast will fail when you
try to assign it to "tb". If the base class's Clone method is clever
and uses reflection to decide what to instantiate, it may work.

As an aside, this is why I implement Clone within my own classes as
Clone plus CopyTo: then I override like this:

public override DerivedClass Clone()
{
DerivedClass obj = new DerivedClass();
this.CopyTo(obj);
return obj;
}

protected override void CopyTo(BaseClass other)
{
base.CopyTo(other);
DerivedClass otherDerived = (DerivedClass)other;
otherDerived._field1 = this._field1;
... etc ...
}

However, if you don't have control over the base class, then you can't
do this.
 
G

Guest

Hi Bruce,

Yip i though that this was the way to go myself but then i did a test
probram where my base class didn't use reflection for clone.
Results were amazing in that this works!

public override object Clone() // derived clone method
{
DerivedTextBox tb = (DerivedTextBox)base.Clone();
tb.MaxValue = this.MaxValue;
tb.MinValue = this.MinValue;
return tb;
}

basically it appears that if implemented this way things it works.
However if i tried to do that outside the class. i.e.
void Main()
{
BaseTextBox tbb = new BaseTextBox();
DerivedTextBox tbd = (DerivedTextBox)tbb.Clone(); // exception thrown

}


So I'm thinking i need to trawl through the language spec or the CLR spec to
find where they say that this is how using base clone works.

Thanks
Brian
 
J

Jon Skeet [C# MVP]

Brian said:
Yip i though that this was the way to go myself but then i did a test
probram where my base class didn't use reflection for clone.
Results were amazing in that this works!

The docs for MemberwiseClone aren't as good as they could be, but the
important bit is:

<quote>
The Type of the clone is the same as the type of the original Object.
</quote>

In other words, it doesn't matter what "level" you call it from, the
type of the cloned object is the same as the type of object you called
it on. So if BaseTextBox.Clone() calls MemberwiseClone() when the
instance is actually a DerivedTextBox, you still get a DerivedTextBox
returned.

Jon
 
G

Guest

Thanks Jon,
Saved the day again.
Needed to know was this standard behaviour or was i by some fluke getting
the same results.

Thanks agian.
regds
Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top