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

To Clone or Not To Clone?

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

Nov 17 '05 #1
4 4294
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.

Nov 17 '05 #2
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:
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.

Nov 17 '05 #3
Brian Keating wrote:
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

Nov 17 '05 #4
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:
Brian Keating wrote:
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

Nov 17 '05 #5

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

Similar topics

0
by: Jason Evans | last post by:
Hi All, I am writing my own implementation of queue via a linked list, note not a LinkedList, and was running into trouble with the clone method. I was wondering if anyone could point out some...
4
by: Vincent | last post by:
Hey, I have a problem to understand the underlaying of clone method. Hope someone can help me out. Let's say, clonedObject = originalObject.clone() (syntax may be wrong, but you know what I...
6
by: Amil Hanish | last post by:
I have two classes that I have implemented ICloneable. From my top-level class, how to I clone the base class values: See "how do I clone the base class values" below. Since Clone returns an...
6
by: Steve | last post by:
I can't find a straight answer on what to use? I need a deep copy, so I implemented IConeable and the Clone() method. However, I'm not sure I did it correct. Is it suposed to be an allocation of...
1
by: Alex D. | last post by:
hi guys. I need to clone multiple times an object and I am succesfully cloning using the regular serialization process, using a MemoryStream. My problem is that after cloning the object more that...
2
by: Steven | last post by:
Hi, I have created my own node (class MyNode : TreeNode) for a TreeView. To populate the treeview, i use something like MyNode newNode = new MyNode("Bla bla bla","0","1") for example. But, to...
16
by: Hamed | last post by:
Hello I am developing a utility to be reused in other programs. It I have an object of type Control (a TextBox, ComboBox, etc.) that other programmers use it in applications. they may set some...
14
by: Hamed | last post by:
Hello It seems that I should implement ICloneable to implement my own clone object. the critical point for me is to make a control object based on another control object that all of its event...
7
by: =?Utf-8?B?Sm9lbCBNZXJr?= | last post by:
I have created a custom class with both value type members and reference type members. I then have another custom class which inherits from a generic list of my first class. This custom listneeds...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.