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

ICloneable

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

However, with Option Strict On, to use this requires

newfoo as foo = CType(oldFoo.Clone(), foo)

This takes additional CPU cycles at runtime - first to coerce foo's clone to
object and then to coerce the clone back to foo.

In the case of a non-inheritable object with a Clone function, is there any
reason to use:

Class foo

Public Function Clone() as foo
' return new_object of type foo
End Function

End Class

Now you can say

option strict on

newfoo = oldFoo.Clone()

Thanks,
Mike Ober.


Jul 21 '05 #1
1 2748
It seems like you just explained the reason to use a strongly typed
version (to avoid the casting to/from object). Did you mean "is there
any reason NOT to use:" ?

The only reason I would say not to use the strongly typed version (as
foo) is if other objects expect your class to implement ICloneable. If
you need to implement ICloneable, you will need to create a Clone() that
returns object. In C# you could have both by explicitly implementing
ICloneable.Clone() - I'm not positive how you would do it in VB.NET. I
think you would have to rename your strongly typed Clone() to something
else, like FooClone().
In the case of a non-inheritable object with a Clone function, is there any
reason to use:

Class foo

Public Function Clone() as foo
' return new_object of type foo
End Function

End Class

Jul 21 '05 #2

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: 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
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: 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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...
0
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...
0
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,...
0
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...

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.