Casting from interface to it's base type

  • Thread starter Thread starter Simon Woods
  • Start date Start date
S

Simon Woods

Hi (final q I hope!)

I have a class which implements an

Public Class MyClass(Of T)
Implements IMyInterface

End Class

The Class was instantiated with a

Dim _myClass as New MyClass(of String)

and then added to a Dictionary(of String, IMyInterface)

When I enumerate the dictionary

For each _kvp as KeyPairValue(of String, IMyInterface)
Dim _myClass = _kvp.value

I look at Intellisense and it tells me that _kvp.Value is a MyClass(of
String), yet when it is assigned to _myClass it is still a IMyInterface.

Is there anyway I can get it to cast to what Intellisense is telling me
it's underlying type is without explicitly

dim _myClass = CType(_kvp.value,MyClass(of String))

Thx

Simon
 
Simon said:
Hi (final q I hope!)

I have a class which implements an

Public Class MyClass(Of T)
Implements IMyInterface

End Class

The Class was instantiated with a

Dim _myClass as New MyClass(of String)

and then added to a Dictionary(of String, IMyInterface)

When I enumerate the dictionary

For each _kvp as KeyPairValue(of String, IMyInterface)
Dim _myClass = _kvp.value

I look at Intellisense and it tells me that _kvp.Value is a MyClass(of
String), yet when it is assigned to _myClass it is still a IMyInterface.

Is there anyway I can get it to cast to what Intellisense is telling me
it's underlying type is without explicitly

dim _myClass = CType(_kvp.value,MyClass(of String))

Thx

Simon

No, you have to cast the reference to the type that you want.

You might be able to make the casting implicit by assigning the
reference to a variable of the type that you want, but that would only
be a difference in the syntax. The casting still has to be performed in
the code, regardless if you make it explicit or implicit.
 
Göran Andersson said:
No, you have to cast the reference to the type that you want.

You might be able to make the casting implicit by assigning the
reference to a variable of the type that you want, but that would only
be a difference in the syntax. The casting still has to be performed in
the code, regardless if you make it explicit or implicit.

OK ... thanks again
 
Back
Top