Hi there,
Consider this from MSDN
*Notes to Inheritors When you derive from DataGridViewChe ckBoxCell 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()
{
NumericDataGrid ViewTextBox tb = (NumericDataGri dViewTextBox)ba se.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 4 4319
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(BaseClas s other)
{
base.CopyTo(oth er);
DerivedClass otherDerived = (DerivedClass)o ther;
otherDerived._f ield1 = this._field1;
... etc ...
}
However, if you don't have control over the base class, then you can't
do this.
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(BaseClas s other) { base.CopyTo(oth er); DerivedClass otherDerived = (DerivedClass)o ther; otherDerived._f ield1 = this._field1; ... etc ... }
However, if you don't have control over the base class, then you can't do this.
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.Clo ne() calls MemberwiseClone () when the
instance is actually a DerivedTextBox, you still get a DerivedTextBox
returned.
Jon
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.Clo ne() calls MemberwiseClone () when the instance is actually a DerivedTextBox, you still get a DerivedTextBox returned.
Jon
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 not so obvious errors I
have made..
Cheers
|
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 mean, I hope )
Why can't I just do this, instead?
|
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 object, what do I assign it to
in my derived class?
public class Base : ICloneable
{
....
public object Clone()
{
|
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 a new object, then the
assignment of each member? Is that all or is there something else I need to
do?
Here is my code
<code>
public Object Clone()
{
|
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 7 or 10 times then my computer's memory
gets flooded and every time I call the Clone() method the processor
resources gets consumed 100% for like 30 seconds. And this problem increases
as the amount of clones are created increases. I read some...
| |
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 move the nodes in the treeview, i use the clone() method... :
MyNode theCopy = (MyNode)theTreeView.SelectedNode.Clone();
|
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 of properties or
assign event handlers. I need to be able to clone the manipulated control at
runtime.
I could use the Clone method of some objects (like Font, Style, String,
|
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 handlers are set like the old
one. Is there a way to do this job?
For example, is there a way to use EventInfo object to get all event
handlers of the old control in runtime and set my new cloned control events
to the event handlers of the old...
|
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 to support cloning:
Public Class RefClass
Public tcp As TcpClient
Public name As String
End Class
Public Class RefClassList
Inherits List(Of RefClass)
Implements ICloneable
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |