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

Implementing System.ICloneable

hi,

the following implementations work:

public class NullSafeCollection: System.Collections.CollectionBase,
System.ICloneable
{
object System.IClonable.Clone()
{
NullSafeCollection clone = new NullSafeCollection();
for (int i = 0; i < this.Count; i++)
clone.Add(this[i]);
return clone;
}
}

public class NullSafeCollection: System.Collections.CollectionBase,
System.ICloneable
{
public object Clone()
{
NullSafeCollection clone = new NullSafeCollection();
for (int i = 0; i < this.Count; i++)
clone.Add(this[i]);
return clone;
}
}
Why can i not use 'public object System.ICloneable.Clone()'?

So what is the difference between naming the methods
'System.IClonable.Clone()' and 'Clone()'?
mfG
--stefan <--
Jan 15 '07 #1
6 4709
"object System.IClonable.Clone() {}" is an explicit implementation of
an interface, so the visibility is implied (either public or internal)
via the interface itself.

Marc
Jan 15 '07 #2
hi Marc,

Marc Gravell wrote:
"object System.IClonable.Clone() {}" is an explicit implementation of
an interface, so the visibility is implied (either public or internal)
via the interface itself.
Thanks, but i still don't see why this works:

public class StraightSegmentList: NullSafeCollection, System.ICloneable
{

public StraightSegmentList Clone()
{
StraightSegmentList clone = new StraightSegmentList();
// I need a shallow copy only.
for (int i = 0; i < this.Count; i++)
clone.Add(this[i]);
return clone;
}

object System.ICloneable.Clone()
{
return this.Clone();
}

Maybe you can enlighten me.

btw, when do i override the MemberwiseClone() method instead of
implementing System.IClonable?

mfG
--stefan <--
Jan 15 '07 #3
Stefan Hoffmann wrote:
btw, when do i override the MemberwiseClone() method instead of
implementing System.IClonable?
You don't override MemberwiseClone - you use it in the
Clone method you add.

Arne
Jan 15 '07 #4
"Stefan Hoffmann" <st*************@explido.dewrote in message
news:eV**************@TK2MSFTNGP02.phx.gbl...
hi Marc,
public class StraightSegmentList: NullSafeCollection, System.ICloneable
{

public StraightSegmentList Clone() -- #1
object System.ICloneable.Clone() -- #2
Consider the following code:
StraightSegmentList list = ...
list.Clone();

ICloneable obj = (ICloneable)list;
obj.Clone();
If someone has a "typed" reference to your object (like the variable 'list')
that invocation of Clone will trigger the Clone() marked #1.
On the otherhand, if someone simply has an instance of ICloneable (like the
variable 'obj'), that invocation of Clone will trigger the Clone() marked
#2.

If all you have is #1 implemented, both types of invocation would call it
(#2 is not required).

As a side note, if all you need is a shallow copy, simply do 'return
MemberwiseClone()' from your clone method.

Hope that helps

--
Adam Clauss
Jan 15 '07 #5
hi Adam,

Adam Clauss wrote:
>public class StraightSegmentList: NullSafeCollection, System.ICloneable
{

public StraightSegmentList Clone() -- #1
>object System.ICloneable.Clone() -- #2

Consider the following code:
StraightSegmentList list = ...
list.Clone();

ICloneable obj = (ICloneable)list;
obj.Clone();
If someone has a "typed" reference to your object (like the variable 'list')
that invocation of Clone will trigger the Clone() marked #1.
On the otherhand, if someone simply has an instance of ICloneable (like the
variable 'obj'), that invocation of Clone will trigger the Clone() marked
#2.
Okay, this makes now sense to me.
If all you have is #1 implemented, both types of invocation would call it
(#2 is not required).
Without #2 it doesn't compile under C#/.Net 1.1 with VS2003.
As a side note, if all you need is a shallow copy, simply do 'return
MemberwiseClone()' from your clone method.
I will test that. Thanx.
mfG
--stefan <--
Jan 15 '07 #6
"Stefan Hoffmann" <st*************@explido.dewrote in message
news:eY**************@TK2MSFTNGP03.phx.gbl...
hi Adam,

Adam Clauss wrote:
>>public class StraightSegmentList: NullSafeCollection, System.ICloneable
{

public StraightSegmentList Clone() -- #1
>>object System.ICloneable.Clone() -- #2
>If all you have is #1 implemented, both types of invocation would call it
(#2 is not required).
Without #2 it doesn't compile under C#/.Net 1.1 with VS2003.
Ahh - didn't catch it the first time. #1 has a return value of
StraightSegmentList rather than object. Thus #1 does not implement the
method defined in the interface - you are correct in that you will need #2.

--
Adam Clauss
Jan 15 '07 #7

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

Similar topics

2
by: eichin | last post by:
One of my recent projects has involved taking an accretion of sh and perl scripts and "doing them right" - making them modular, improving the error reporting, making it easier to add even more...
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...
3
by: scoobydoo | last post by:
Hello, I am trying to implement ICloneable's Clone() function, using Serialization. However, my code causes an exception. I have a class derived from TreeNode called "Node1". In Node1, I...
8
by: hex | last post by:
Hi I make a class "MyClass" and this clas implements the Interface ICloneable. I want when I instance an object from MyClass and I call obj.Clone() it returns an object of MyClass type. for...
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: 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...
0
by: Jeremy Chapman | last post by:
I am thinking of implementing System.Web.UI.SessionPageStatePersister as a method of storing viewstate in the session. Before I do, I am browsing threw the class's code using reflector just to get...
6
by: satyamedicherla | last post by:
Hi All, We have more than 50 Crystal Reports (Implemented) in our project. Please let us know how to implement Cascading Style Sheet to Text Objects and Line Objects in Crystal Reports and...
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: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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)...
0
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...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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.