Casting from interface to it's base type


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
 
Ad

Advertisements

G

Göran Andersson

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.
 
Ad

Advertisements

S

Simon Woods

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
 

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