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

ICloneable ... why type it out?

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 method "Clone" ... it's just a naming habit I got into
with VB5/6 using LSET on UDTs for serializing state and creating copies of
objects.

The way I do it is of course with .Net is the wya most everyone does it,
mark the class with the Serializable attribute, implement ISserializable
with it's constructor and GetObjectData. The Clone method does the
traditional binaryformater and memory stream serialization and the method
returning an object of the same type as the running instance (though I often
overload the Clone method and use custom serialization to instead convert
the object to another type, such as a state-only object for tighter
remoting). No rule breaking here, ISerializable's "propertybagish" system
is logical.

But, here's the question: In my three years of primarily doing .Net, most of
my clones (found in nearly every one of my apps) never specifically
implemented ICloneable. I'll add it if I'm feelign picky but even when I do
add it, I always just look at that interface and scratch my head ... There
seems to be nothing worthwhile in using it, since I'm doing all of the
actual work myself, using the syntax (albeit out of pre-.Net habit) and
just don't need the "reminder" that I have a Clone method. Also, my Clones
output an actual type rather than an Object.

Forgive my ignorance, but can you tell me a real reason for specifically
typing out the "Implements ICloneable", beyond just "it's what you're
supposed to do"?

There's doing "the right thing" for a good reason and then there's going
overboard ... in this little thing I wonder the basic logic.

Thanks.

Robert Smith
Kirkland, WA
www.smithvoice.com


Jul 21 '05 #1
4 1911
Consistency perhaps? I'd say that people consuming your libraries (if that
were the case) would expect to use a standard CLR interface to achieve
cloning. Not that you couldn't do it the way you describe, but still.

It's kinda like always inheriting from EventArgs when creating complex types
to pass to delegate handlers. You don't have to, but it's nice to "stick" to
the "standard", I guess.
--
Klaus H. Probst, MVP
http://www.vbbox.com/
"smith" <rc********@smithvoiceTAKEOUT.com> wrote in message
news:OJ****************@newsread3.news.pas.earthli nk.net...
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 method "Clone" ... it's just a naming habit I got into
with VB5/6 using LSET on UDTs for serializing state and creating copies of
objects.

The way I do it is of course with .Net is the wya most everyone does it,
mark the class with the Serializable attribute, implement ISserializable
with it's constructor and GetObjectData. The Clone method does the
traditional binaryformater and memory stream serialization and the method
returning an object of the same type as the running instance (though I often overload the Clone method and use custom serialization to instead convert
the object to another type, such as a state-only object for tighter
remoting). No rule breaking here, ISerializable's "propertybagish" system
is logical.

But, here's the question: In my three years of primarily doing .Net, most of my clones (found in nearly every one of my apps) never specifically
implemented ICloneable. I'll add it if I'm feelign picky but even when I do add it, I always just look at that interface and scratch my head ... There seems to be nothing worthwhile in using it, since I'm doing all of the
actual work myself, using the syntax (albeit out of pre-.Net habit) and
just don't need the "reminder" that I have a Clone method. Also, my Clones output an actual type rather than an Object.

Forgive my ignorance, but can you tell me a real reason for specifically
typing out the "Implements ICloneable", beyond just "it's what you're
supposed to do"?

There's doing "the right thing" for a good reason and then there's going
overboard ... in this little thing I wonder the basic logic.

Thanks.

Robert Smith
Kirkland, WA
www.smithvoice.com

Jul 21 '05 #2
Thanks Klaus (and good to talk to you again). I do understand the tradition
of it and I grasp the idea (while Inheriting and implementing an interface
are a little bit different) but this particual interface just strikes me as
less than real-world logical for some reason.

Perhaps the aspect that bothers me about it most is that the return type of
the interface's Clone method is "Object" when it seems to me a Clone should
return an object of the exact type of the source and casting shouldn't be
needed; Granted the only way to generalize is to make the return an "Object"
but since in this case there's only just the one method in the interface ...
oy, Even right now as I try to just accept it as "what you should do because
that's how it's done" it just continues to look like a silly interface.

It's not that it's a major deal, my code hasn't failed over it and others do
tap into my dlls. I just see newer devs just catching the .Net religion and
following little things like this as if they're some rule from God and
wonder if there's some tangible performance issue I've been missing out on.

Thanks.

-smith
www.smithvoice\.com

"Klaus H. Probst" <us*******@vbbox.com> wrote in message
news:e5**************@TK2MSFTNGP14.phx.gbl...
Consistency perhaps? I'd say that people consuming your libraries (if that
were the case) would expect to use a standard CLR interface to achieve
cloning. Not that you couldn't do it the way you describe, but still.

It's kinda like always inheriting from EventArgs when creating complex
types
to pass to delegate handlers. You don't have to, but it's nice to "stick"
to
the "standard", I guess.
--
Klaus H. Probst, MVP
http://www.vbbox.com/

Jul 21 '05 #3
>
It's not that it's a major deal, my code hasn't failed over it and others
do tap into my dlls. I just see newer devs just catching the .Net
religion and following little things like this as if they're some rule
from God and wonder if there's some tangible performance issue I've been
missing out on.


No performance issues, just potential consistency and compatibility issues.
Most interfaces exist for the benefit of API's and\or languages(usually
api's). It is far easier, faster, and safer to determine Clone generically
via an ICloneable interface than it is, say, via reflection.
If you had a generic object manager(not specialized to a specific tree) that
clones objects to maintain a pool or a language that provides a "copy" or
"clone" keyword, both of those would likely rely on ICloneable in a generic
sense, or would have anyway.

The issue with ICloneable is that it doesn't specify deep or shallow copy.
Its use is...less valuable that the designers probably intended. I would
pretty strongly suggest foregoing ICloneable and using your own cloning
method\interface to specify clone functionality. Just remember to specify if
the method is deep, shallow, or object-specific. Being unsure of
expectations is a considerable problem.

I would certainly, however, recommend an interface or common base class
instead of just adding Clone methods everywhere. This may not be feasible
until the 2.0 release, however.

Jul 21 '05 #4
Always sataisfying to talk things out. Thank you Daniel.
-s

"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:%2****************@TK2MSFTNGP11.phx.gbl...
No performance issues, just potential consistency and compatibility
issues. Most interfaces exist for the benefit of API's and\or
languages(usually api's). It is far easier, faster, and safer to determine
Clone generically via an ICloneable interface than it is, say, via
reflection.
If you had a generic object manager(not specialized to a specific tree)
that clones objects to maintain a pool or a language that provides a
"copy" or "clone" keyword, both of those would likely rely on ICloneable
in a generic sense, or would have anyway.

The issue with ICloneable is that it doesn't specify deep or shallow copy.
Its use is...less valuable that the designers probably intended. I would
pretty strongly suggest foregoing ICloneable and using your own cloning
method\interface to specify clone functionality. Just remember to specify
if the method is deep, shallow, or object-specific. Being unsure of
expectations is a considerable problem.

I would certainly, however, recommend an interface or common base class
instead of just adding Clone methods everywhere. This may not be feasible
until the 2.0 release, however.

Jul 21 '05 #5

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

Similar topics

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...
6
by: Andy | last post by:
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...
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
8
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: 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 =...
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...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
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: 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...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.