G
Guest
I'm new to dealing with interfaces. I'm not sure what is wrong with this. I
get an System.InvalidCastException when I try to run it. I setup 2 classes
with the same interface, then try to cast one into the other. Shouldn't all
the common interface fields cast into the other? I'm sure I'm doing
something stupid. Thanks for your help!!!
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim class1 As New ClassA("1")
Dim class2 As New ClassB
'I get an "System.InvalidCastException" ??? Why????
class2 = CType(class1, IMyCommonStuff)
End Sub
End Class
Public Interface IMyCommonStuff
Property PropertyA() As String
Function MethodA() As String
End Interface
Public Class ClassA
Implements IMyCommonStuff
Public Sub New(ByVal propertyA As String)
_PropertyA = propertyA
End Sub
Public Function MethodA() As String Implements IMyCommonStuff.MethodA
Return "In Mehtod A"
End Function
Dim _PropertyA As String
Public Property PropertyA() As String Implements IMyCommonStuff.PropertyA
Get
Return _PropertyA
End Get
Set(ByVal Value As String)
_PropertyA = PropertyA
End Set
End Property
End Class
Public Class ClassB
Implements IMyCommonStuff
Public Function MethodA() As String Implements IMyCommonStuff.MethodA
Return "some stuff"
End Function
Dim _propertyA As String
Public Property PropertyA() As String Implements IMyCommonStuff.PropertyA
Get
Return _propertyA
End Get
Set(ByVal Value As String)
_propertyA = Value
End Set
End Property
Dim _anotherClassBProperty As Integer
Public Property AnotherClassBProperty() As Integer
Get
Return _anotherClassBProperty
End Get
Set(ByVal Value As Integer)
_anotherClassBProperty = Value
End Set
End Property
End Class
get an System.InvalidCastException when I try to run it. I setup 2 classes
with the same interface, then try to cast one into the other. Shouldn't all
the common interface fields cast into the other? I'm sure I'm doing
something stupid. Thanks for your help!!!
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim class1 As New ClassA("1")
Dim class2 As New ClassB
'I get an "System.InvalidCastException" ??? Why????
class2 = CType(class1, IMyCommonStuff)
End Sub
End Class
Public Interface IMyCommonStuff
Property PropertyA() As String
Function MethodA() As String
End Interface
Public Class ClassA
Implements IMyCommonStuff
Public Sub New(ByVal propertyA As String)
_PropertyA = propertyA
End Sub
Public Function MethodA() As String Implements IMyCommonStuff.MethodA
Return "In Mehtod A"
End Function
Dim _PropertyA As String
Public Property PropertyA() As String Implements IMyCommonStuff.PropertyA
Get
Return _PropertyA
End Get
Set(ByVal Value As String)
_PropertyA = PropertyA
End Set
End Property
End Class
Public Class ClassB
Implements IMyCommonStuff
Public Function MethodA() As String Implements IMyCommonStuff.MethodA
Return "some stuff"
End Function
Dim _propertyA As String
Public Property PropertyA() As String Implements IMyCommonStuff.PropertyA
Get
Return _propertyA
End Get
Set(ByVal Value As String)
_propertyA = Value
End Set
End Property
Dim _anotherClassBProperty As Integer
Public Property AnotherClassBProperty() As Integer
Get
Return _anotherClassBProperty
End Get
Set(ByVal Value As Integer)
_anotherClassBProperty = Value
End Set
End Property
End Class