Connecting Tech Pros Worldwide Help | Site Map

To Clone or Not To Clone?

Brian Keating
Guest
 
Posts: n/a
#1: Nov 17 '05
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

Bruce Wood
Guest
 
Posts: n/a
#2: Nov 17 '05

re: To Clone or Not To Clone?


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.

Brian Keating
Guest
 
Posts: n/a
#3: Nov 17 '05

re: To Clone or Not To Clone?


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

"Bruce Wood" wrote:
[color=blue]
> 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.
>
>[/color]
Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#4: Nov 17 '05

re: To Clone or Not To Clone?


Brian Keating wrote:[color=blue]
> 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![/color]

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

Brian Keating
Guest
 
Posts: n/a
#5: Nov 17 '05

re: To Clone or Not To Clone?


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

"Jon Skeet [C# MVP]" wrote:
[color=blue]
> Brian Keating wrote:[color=green]
> > 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![/color]
>
> 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
>
>[/color]
Closed Thread