Results 1 to 9 of 9

Thread: ICloneable, cant change clone type?!

  1. #1

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    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:
    1. Public Class someWeirdClass
    2.     Implements ICloneable
    3.  
    4.     Public Overloads Function Clone() As [COLOR=RED][b]someClass[/b][/COLOR] Implements ICloneable.Clone
    5.  
    6.     End Function
    7. 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!!

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    You'd have to implement it as object then overload it with a strong typed version as well.

    VB Code:
    1. Public Class someWeirdClass
    2.     Implements ICloneable
    3.  
    4.     Public Overloads Function Clone() As Object Implements ICloneable.Clone
    5.  
    6.     End Function
    7.  
    8.     Public Overloads Function Clone() As someWeirdClass
    9.  
    10.     End Function
    11. 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.

  3. #3

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Thumbs up

    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?
    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!!

  4. #4
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    Or just simply cast it using CType()

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

  5. #5

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    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?!
    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!!

  6. #6
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    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.

  7. #7

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    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!!

  8. #8
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    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.

  9. #9

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    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
    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!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width