|
-
Oct 21st, 2002, 10:58 PM
#1
ICloneable, cant change clone type?!
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:
VB Code:
Public Class someWeirdClass
Implements ICloneable
Public Overloads Function Clone() As [COLOR=RED][b]someClass[/b][/COLOR] 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
rate my posts if they help ya!
Extract thumbnail without reading the whole image file: (C# - VB)
Apply texture to bitmaps: (C# - VB)
Extended console library: (VB)
Save JPEG with a certain quality (image compression): (C# - VB )
VB.NET to C# conversion tips!!
-
Oct 21st, 2002, 11:18 PM
#2
You'd have to implement it as object then overload it with a strong typed version as well.
VB Code:
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.
-
Oct 21st, 2002, 11:20 PM
#3
-
Oct 21st, 2002, 11:23 PM
#4
PowerPoster
Or just simply cast it using CType()
** Sorry Edeenis, just saw your comment at the bottom **
-
Oct 21st, 2002, 11:25 PM
#5
-
Oct 21st, 2002, 11:27 PM
#6
PowerPoster
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.
-
Oct 21st, 2002, 11:30 PM
#7
makes sense
how come I cant change the return type though?
rate my posts if they help ya!
Extract thumbnail without reading the whole image file: (C# - VB)
Apply texture to bitmaps: (C# - VB)
Extended console library: (VB)
Save JPEG with a certain quality (image compression): (C# - VB )
VB.NET to C# conversion tips!!
-
Oct 21st, 2002, 11:30 PM
#8
PowerPoster
Try this:
Code:
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
Last edited by Lethal; Oct 21st, 2002 at 11:39 PM.
-
Oct 21st, 2002, 11:33 PM
#9
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|