|
-
Dec 14th, 2010, 02:22 PM
#1
Thread Starter
New Member
Casting Between Derived Types Throws InvalidCastException
The following Code throws the InvalidCastException error. What I wish to know is why and what the standard workaround is for this situation.
Code:
Module Module1
Interface IFooBar
End Interface
Class Foo : Implements IFooBar
Public Shared Widening Operator CType(ByVal Val As Foo) As Bar
Return New Bar
End Operator
End Class
Class Bar : Implements IFooBar
Public Shared Widening Operator CType(ByVal Val As Bar) As Foo
Return New Foo
End Operator
End Class
Sub Main()
Dim A As Foo = New Foo
Dim B As Bar = New Bar
Dim C As IFooBar
'these assignments work fine with implicit casting
A = B
B = A
C = A
'InvalidCastException Thrown Here
B = C
'this throws the same error
B = CType(C, Bar)
'this does not fail but requires pre-compile knowledge of the type of object in C
B = CType(CType(C, Foo), Bar)
End Sub
End Module
What I would like is the line B = C to work without me having to know what type of object C currently is. I need to find a way to get this to work so that i can finish a function I ma working on so that it will excecute correctly on any new object which implements the interface and boasts an Operator CType function to cast it to the type used in the function.
I have been searching a while for a solution but I couldn't find one. It may be that I am missing a key term which would get me straight to the answer. Any help you can give me would be much appreciated. Although I would particularly appreciate some references to articles which might explain this problem and why it occurs as I am just learning Visual Basic.
-
Dec 14th, 2010, 03:18 PM
#2
Re: Casting Between Derived Types Throws InvalidCastException
I don't think what you're trying to do is possible. Interface types are generic. You can't assign one to a specific type (SpecificType=InterfaceType)
I've used Widening operators to do automatic type conversion, but I don't see why you'd ever need to do what you're trying to do. Wouldn't an inherited type be better? Regardless, you'll need to have your function take a base-type and check the type of what you're handing it to determine if it's a Foo or a Bar and how to proceed. If you have common functionality between them though, they should be handled in the basetype; perhaps overridable, but still defined in the basetype. Example:
Code:
Public Class BaseFooBar
Public Overridable Sub BaseClassFunction()
End Sub
End Class
Public Class Foo
Inherits BaseFooBar
Public Shared Widening Operator CType(ByVal Val As Bar) As Foo
Return New Foo
End Operator
Public Overrides Sub BaseClassFunction()
MyBase.BaseClassFunction()
End Sub
End Class
Public Class Bar
Inherits BaseFooBar
Public Shared Widening Operator CType(ByVal Val As Foo) As Bar
Return New Bar
End Operator
Public Overrides Sub BaseClassFunction()
MyBase.BaseClassFunction()
End Sub
End Class
Public Module Blah
Public Sub Main()
Dim A As Foo = New Foo
Dim B As Bar = New Bar
Dim C As BaseFooBar
A = B
B = A
C = A
MyFunc(A)
MyFunc(B)
MyFunc(C)
End Sub
Public Sub MyFunc(ByVal Foobar As BaseFooBar)
Foobar.BaseClassFunction()
End Sub
End Module
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
|