views:

205

answers:

1

Hi! Sorry if this question is very very basic.

I need to cast, in the better way, 2 objects of 2 types of 2 custom-classes (in VB.Net):

The code:

Public Class pluto
    Public Sub New(ByVal campoPippoPass As String)
        _campoPippo = campoPippoPass
    End Sub

    Private _campoPippo As String = ""

    Public Property campoPippo() As String
        Get
            Return Me._campoPippo
        End Get
        Set(ByVal value As String)
            If Not Object.Equals(Me._campoPippo, value) Then
                Me._campoPippo = value
            End If
        End Set
    End Property
End Class

Public Class pippo
    Public Sub New(ByVal campoPippoPass As String)
        _campoPippo = campoPippoPass
    End Sub

    Private _campoPippo As String = ""

    Public Property campoPippo() As String
        Get
            Return Me._campoPippo
        End Get
        Set(ByVal value As String)
            If Not Object.Equals(Me._campoPippo, value) Then
                Me._campoPippo = value
            End If
        End Set
    End Property
End Class

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Dim a As New pippo("ciao")
    ' here I have the error 'invalid cast'
    Dim c As pluto = CType(a, pluto)
    MsgBox(c.campoPippo)
End Sub

How can I convert "c" in an "a" type object? There is another way than writing this? Dim c As New pluto(a.campoPippo) In the case of more complex class, it could be more easy if there were a function for the conversion. Thank you! Pileggi

+1  A: 

Firstly, I'm assuming the line: Dim c As pluto = CType(b, pluto) is a mistype and should actually be Dim c As pluto = CType(a, pluto)?

You can't cast one class to another unless they're related. You might need to explain what you're trying to do otherwise my answer would be, why are you creating the different classes pluto and pippo if they seem to be identical? Just create the one class and create two objects of it.

If you do need separate classes, maybe they are related in some way and you could make pippo inherit from pluto? Or make both of them implement the same interface.

In general I'd also suggest that it might be worth translating your class/variable names to English since that might make it easier for people to understand what you're trying to do.

ho1
Ok, if one class inherits the other I can do the conversion, but if both of them implement the same interface, the conversion fails. Why?
pileggi
@pileggi: If both classes implement the same interface it won't make you able to cast between the classes, but you can then store them in a variable of the type of the interface. So lets say that you're interface is called `ITest`, then you could use the line `Dim c As ITest = CType(a, ITest)` and if the `campoPippo` method is part of the interface, you can then call `c.campoPippo()` no matter which of the classes c actually contains.
ho1
Thank you very much! Then I can conclude the only way to make one class convertible to another is to do the inherits of one of them to the other. Right?
pileggi
@pileggi: Well, you can create Conversion Operators also. See here for details: http://msdn.microsoft.com/en-us/library/yf7b9sy7%28VS.80%29.aspx
ho1
Great! Thank you!!
pileggi