PDA

Click to See Complete Forum and Search --> : ICloneable, cant change clone type?!


MrPolite
Oct 21st, 2002, 10:58 PM
well I want my class to have a clone function, but I dont want it to return an Object. I've seen some of .NET's classes which are cloneable and yet their .Clone method doesnt return an object. I have this code:

Public Class someWeirdClass
Implements ICloneable

Public Overloads Function Clone() As someClass Implements ICloneable.Clone

End Function
End Class


it gives me this error: "'Clone' cannot implement 'Clone' because there is no matching function on interface 'ICloneable'."

so what should I do if I want the clone method to return something of type someClass? I dont want to return an object:(

Edneeis
Oct 21st, 2002, 11:18 PM
You'd have to implement it as object then overload it with a strong typed version as well.


Public Class someWeirdClass
Implements ICloneable

Public Overloads Function Clone() As Object Implements ICloneable.Clone

End Function

Public Overloads Function Clone() As someWeirdClass

End Function
End Class


Although why do you not want it to return an object? If you need a strong type you could just convert it after the clone.

MrPolite
Oct 21st, 2002, 11:20 PM
:) thanks!
umm, I have some classes that inherit from this. I think there is no need that they should return an object. They would all have to override the clone method.... anyways why would you return an object?!


and why does my class have to implement the iclonable method? what would be the difference if I had my own clone function and it didnt implement from iclonable at all?

Lethal
Oct 21st, 2002, 11:23 PM
Or just simply cast it using CType()

** Sorry Edeenis, just saw your comment at the bottom **

MrPolite
Oct 21st, 2002, 11:25 PM
eeeh Edneeis your example doesnt work :( :( :(


about Ctype thingie, isn't it just better to return the type that I want and not to convert it?!

Lethal
Oct 21st, 2002, 11:27 PM
Because you are implementing an interface, you are therefore binded to a contract stating that you will support (implement) all of the declarations exposed by the interface. You would implement this interface rather than creating your own clone method b/c it is a .NET standard and its something that everyone understands.

MrPolite
Oct 21st, 2002, 11:30 PM
makes sense:)


how come I cant change the return type though?

Lethal
Oct 21st, 2002, 11:30 PM
Try this:


Public Class CloneExample
Implements ICloneable
Public Name As String

Public Overloads Function Clone() As Object Implements ICloneable.Clone
Dim objClone As Object = Me.MemberwiseClone()
Return CType(objClone, CloneExample)
End Function
End Class

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim obj As CloneExample = New CloneExample()
obj.Name = "Bob"

Dim obj2 As CloneExample = obj.Clone()
obj2.Name = "Smith"

MessageBox.Show(obj.Name)
MessageBox.Show(obj2.Name)
End Sub

MrPolite
Oct 21st, 2002, 11:33 PM
hehe, I already have this working this way :) I just wanted to see if I can change the return type to my own type. Sounds like I cant :(