473,671 Members | 2,557 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1949
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********@smi thvoiceTAKEOUT. com> wrote in message
news:OJ******** ********@newsre ad3.news.pas.ea rthlink.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*******@vbbo x.com> wrote in message
news:e5******** ******@TK2MSFTN GP14.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(usual ly
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\interfac e 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******** ********@TK2MSF TNGP11.phx.gbl. ..
No performance issues, just potential consistency and compatibility
issues. Most interfaces exist for the benefit of API's and\or
languages(usual ly 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\interfac e 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
2766
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 foo End Function End Class
1
3107
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 constructor an array of type FileNode, when it copies the FileNode array, will it call Clone on each FileNode object, or just do a MemberwiseClone of each FileNode? I'm hoping that in the constructor they do somethine like try casting each...
6
3235
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 typed definition. This means that the class you create exposes a method that returns an object which the user of the class must cast.
2
2254
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 frmLogin(foUser)
7
26453
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
366
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 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...
1
2293
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.. Thank you so much!
2
19464
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 ICloneable MyObject va3 = (MyObject)var1.Clone(); Does anyone know why the 2.0 framework doesn't include a Generic implementation of the ICloneable interface?
6
4734
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 = new NullSafeCollection();
0
8483
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, 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...
0
8927
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8676
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 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...
0
7445
agi2029
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...
1
6237
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5703
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();...
0
4416
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2062
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1816
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.