473,287 Members | 2,263 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,287 software developers and data experts.

Advantages of ICloneable?

Hello.

I've got an issue with the ICloneable interface that makes me think I'm
missing the advantages of it.

My issue is that the implemented Clone method returns an object and not
a strongly typed definition. This means that the class you create
exposes a method that returns an object which the user of the class must
cast.

public object Clone() {...}

I would much prefer the class to expose a type of itself:

public MyClass Clone() {...}

so that the caller doesn't have to cast and the interface is cleaner.

Why should I then use IClonable??
Thanks,
Andy
Nov 17 '05 #1
6 3217
Andy <an*******@hotmail.com> wrote:
I've got an issue with the ICloneable interface that makes me think I'm
missing the advantages of it.

My issue is that the implemented Clone method returns an object and not
a strongly typed definition. This means that the class you create
exposes a method that returns an object which the user of the class must
cast.

public object Clone() {...}

I would much prefer the class to expose a type of itself:

public MyClass Clone() {...}

so that the caller doesn't have to cast and the interface is cleaner.
You can expose that separately, and implement ICloneable explicitly.
Why should I then use IClonable??


So that things which only care that an object can be cloned can accept
instances of your class as a parameter. Not everything will always know
your concrete type...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2

One standard impelementation is something like this:

public MyClass Clone() { ... }

object ICloneable.Clone { return Clone(); }

That way if the reference is typed as MyClass you can use the strongly
typed Clone method, but if you're referring to the object simply as
ICloneable then you can use the standard interface method.

This implementation gives you the best of both worlds--a strongly
typed Clone() method and also a polymorhpic Clone() method.

HTH,

Sam
On Thu, 26 May 2005 20:46:11 GMT, Andy <an*******@hotmail.com> wrote:
Hello.

I've got an issue with the ICloneable interface that makes me think I'm
missing the advantages of it.

My issue is that the implemented Clone method returns an object and not
a strongly typed definition. This means that the class you create
exposes a method that returns an object which the user of the class must
cast.

public object Clone() {...}

I would much prefer the class to expose a type of itself:

public MyClass Clone() {...}

so that the caller doesn't have to cast and the interface is cleaner.

Why should I then use IClonable??
Thanks,
Andy


Nov 17 '05 #3
Jon Skeet [C# MVP] wrote:
Andy <an*******@hotmail.com> wrote:
I've got an issue with the ICloneable interface that makes me think I'm
missing the advantages of it.

My issue is that the implemented Clone method returns an object and not
a strongly typed definition. This means that the class you create
exposes a method that returns an object which the user of the class must
cast.

public object Clone() {...}

I would much prefer the class to expose a type of itself:

public MyClass Clone() {...}

so that the caller doesn't have to cast and the interface is cleaner.

You can expose that separately, and implement ICloneable explicitly.

Why should I then use IClonable??

So that things which only care that an object can be cloned can accept
instances of your class as a parameter. Not everything will always know
your concrete type...


That makes sense.

Thank you.
Nov 17 '05 #4
Good tip.

Thank you.

Samuel R. Neff wrote:
One standard impelementation is something like this:

public MyClass Clone() { ... }

object ICloneable.Clone { return Clone(); }

That way if the reference is typed as MyClass you can use the strongly
typed Clone method, but if you're referring to the object simply as
ICloneable then you can use the standard interface method.

This implementation gives you the best of both worlds--a strongly
typed Clone() method and also a polymorhpic Clone() method.

HTH,

Sam
On Thu, 26 May 2005 20:46:11 GMT, Andy <an*******@hotmail.com> wrote:

Hello.

I've got an issue with the ICloneable interface that makes me think I'm
missing the advantages of it.

My issue is that the implemented Clone method returns an object and not
a strongly typed definition. This means that the class you create
exposes a method that returns an object which the user of the class must
cast.

public object Clone() {...}

I would much prefer the class to expose a type of itself:

public MyClass Clone() {...}

so that the caller doesn't have to cast and the interface is cleaner.

Why should I then use IClonable??
Thanks,
Andy


Nov 17 '05 #5
Here's another tip: in an inheritance hierarchy, implementing
ICloneable can be a pain:

public class BaseClass : ICloneable
{
private int _member1;
private string _member2;

public BaseClass(string aString)
{
this._member2 = aString;
this._member1 = 0;
}

public virtual object Clone()
{
BaseClass clone = new BaseClass(this._member2);
clone._member1 = this._member1;
...
}
}

public class DerivedClass : BaseClass
{
}

Now, what you really want to do in DerivedClass is override Clone().
However, there's a problem: you can't get at the fields in the base
class, because they're private, and the base class doesn't necessarily
expose all of those fields as properties, or allow you to set them. So,
how do you clone the derived class (including the base class fields) if
you can't copy the base class fields?

The answer is to make a protected method called CopyTo (or CopyFrom...
it doesn't really much matter), and a protected empty constructor.

public class BaseClass : ICloneable
{
private int _member1;
private string _member2;

public BaseClass(string aString)
{
this._member2 = aString;
this._member1 = 0;
}

protected BaseClass()
{
this._member2 = null;
this._member1 = 0;
}

protected virtual CopyFrom(BaseClass otherBase)
{
this._member1 = otherBase._member1;
this._member2 = otherBase._member2;
...
}

public virtual object Clone()
{
BaseClass clone = new BaseClass();
clone.CopyFrom(this);
}
}

Now the derived class looks similar:

public class DerivedClass : BaseClass
{
private int _member3;
private string _member4;

public DerivedClass(string aString) : base(aString)
{
this._member3 = "";
this._member4 = 0;
}

protected DerivedClass() : BaseClass()
{
this._member3 = null;
this._member4 = 0;
}

protected virtual CopyFrom(DerivedClass otherDerived)
{
base.CopyFrom(otherDerived);
this._member3 = otherDerived._member3;
this._member4 = otherDerived._member4;
...
}

public virtual object Clone()
{
DerivedClass clone = new DerivedClass();
clone.CopyFrom(this);
}
}

Nov 17 '05 #6
Bruce Wood <br*******@canada.com> wrote:
Here's another tip: in an inheritance hierarchy, implementing
ICloneable can be a pain:


<snip>

Alternatively, where it's appropriate (i.e. for shallow copies where
you have enough access to make any "interesting" bits deep yourself)
you can just use Object.MemberwiseClone.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #7

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

Similar topics

4
by: smith | last post by:
I've been making deep copy clones of objects since my second week or so with ..Net (it took me the first week to realize that shallow copies were usually worthless as "clones"). Normally I call my...
1
by: Michael D. Ober | last post by:
In VB 2005, the ICloneable interface requires the following: Class foo Implements ICloneable Public Function Clone() as Object Implements System.ICloneable.Clone ' return new_object of type...
1
by: Hasani | last post by:
I have an object called FileNode and it implements ICloneable which creates a deep copy of the FileNode. The one of the constructors for ArrayList use an ICollection as a paremeter. If I pass that...
2
by: Venkat Venkataramanan | last post by:
Hello: I have an object Users that implements the ICloneable interface. I am trying to pass an instance of this object, foUser from one form into a second form ByRef. Dim formLogin As New...
7
by: Don | last post by:
Can anyone give me an example of implementing ICloneable to give a class I created a "Clone" method so I can make copies of objects. I have no idea where to begin with this. Thanks. - Don
1
by: Rain | last post by:
Hi. Im a C# newbie, just want to ask how to implement the ICloneable.Clone.. Dont know how it works, would really appreciate it if someone could show a simple source sample of how to do this.....
2
by: Nathan | last post by:
I'm working with Clone() for the first time, and noticed that you have to unbox the Clone of an object that implements ICloneable: MyObject var1 = new MyObject(); // Where MyObject implements...
6
by: Stefan Hoffmann | last post by:
hi, the following implementations work: public class NullSafeCollection: System.Collections.CollectionBase, System.ICloneable { object System.IClonable.Clone() { NullSafeCollection clone =...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

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.