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

Home Posts Topics Members FAQ

Icloneable

Hi. Im a C# newbie, just want to ask how to implement the ICloneable.Clon e..
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!
Mar 2 '06 #1
1 2290

"Rain" <Ra**@discussio ns.microsoft.co m> wrote in message
news:7A******** *************** ***********@mic rosoft.com...
Hi. Im a C# newbie, just want to ask how to implement the
ICloneable.Clon e..
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!


public class X : ICloneable
{
// a represents here whatever the internal state of the object is
private int a;

// you may want a private ctor specifically for this purpose
public X(int p) { a = p;}

// allows X x2 = x1.Clone();
// visible in intellisense
public X Clone() { return new X(a); }

// allows X x2 = (X)((ICloneable )x1).Clone();
// NOT visible in intellisense for an object of compile time type X
object ICloneable.Clon e()
{
return Clone();
}
}

This is an explicit implementation of the interface with a typesafe method
as well - I recommend this approach be used always as it is much more user
friendly than just just implementing the interface "normally":

public class X : ICloneable
{
private int a;

public X(int p) { a = p;}

// allows X x2 = (X)x1.Clone();
// visible in intellisense
public object Clone() { return new X(a); } }
Mar 2 '06 #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
2762
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
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
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
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...
0
8329
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
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...
1
6010
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
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...
0
4022
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1580
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
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.