Interace questions

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
 
P

Patrice

You still try to cast Class1 to a Class2 variable. As they have common
interface you can cast both Class1 and Class2 to a IMyCommonStuff variable
(but you still can't cast Class1 to Class2 or the other way round as they
have only a common *subset* ) :

Dim IMCS As IMyCommonStuff

IMCS=CTYpe(Class1,IMyCommonStuff)
IMCS.MethodA

IMCS=CTYpe(Class2,IMyCommonStuff)
IMCS.MethodA

Note that you can never call the B specific method through ICMS. You see
only what is common...
 
J

Jon Skeet [C# MVP]

PushPinOuch said:
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!!!

Firstly, you should almost *always* be working with Option Strict On -
at which point this won't even compile.

Secondly, no, it shouldn't work. The type of the class2 variable is
ClassB. It won't accept any old IMyCommonStuff.

If, however, you declare it to be of type IMyCommonStuff, it'll work
fine, and you won't even need to cast.

You can't try to pretend that an object of one type is actually an
object of a different type - you *can* however, use objects in terms of
the interfaces they implement.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top