473,246 Members | 1,417 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,246 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 4706
"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: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
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...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
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: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...

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.