473,608 Members | 2,592 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ICloneable

In VB 2005, the ICloneable interface requires the following:

Class foo
Implements ICloneable

Public Function Clone() as Object Implements System.ICloneab le.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.Cl one(), 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 2762
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.Clon e() - 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
1948
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
3105
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
2249
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
26445
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
223
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
2290
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
19460
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
4732
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
8057
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
7998
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,...
0
8491
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...
1
8142
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,...
0
6813
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...
0
5475
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
3959
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...
1
2472
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
0
1327
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.