Problem with type conversion/inference

  • Thread starter Maurizio Colucci
  • Start date
M

Maurizio Colucci

Hi,

I have a function so defined

function foo (byval l as List(of IMyInterface)) as bar

I have a List(of C), where C implements IMyInterface.

I want to pass the list as input to foo, like


dim l = new list(of C)
dim result = foo(l)

but this does not compile (with option infer on). It seems VB can't
understand that a list of C is also a list of IMyInterface.

Am I missing something or type inference in VB is flawed? What
workaround do you suggest?

Thank you,

Maurizio
 
A

Armin Zingler

Maurizio Colucci said:
Hi,

I have a function so defined

function foo (byval l as List(of IMyInterface)) as bar

I have a List(of C), where C implements IMyInterface.

I want to pass the list as input to foo, like


dim l = new list(of C)
dim result = foo(l)

but this does not compile (with option infer on). It seems VB can't
understand that a list of C is also a list of IMyInterface.

Am I missing something or type inference in VB is flawed? What
workaround do you suggest?


Should work at first sight, however: Imagine, inside function foo, you were
able to add an object to the list that implements IMyInterface. For examle,
class D also implements IMyInterface. But, the list object is actually a
List(of C). You would add D objects to a List(of C) which must not work.
Therefore it is not possible to call the function passing a List(Of C), that
means, a List(Of C) is NOT derived from a List(Of IMyInterface) just because
C implements IMyInterface.


Armin
 
M

Maurizio Colucci

Should work at first sight, however: Imagine, inside function foo, you were
able to add an object to the list that implements IMyInterface. For examle,
class D also implements IMyInterface. But, the list object is actually a
List(of C). You would add D objects to a List(of C) which must not work.
Therefore it is not possible to call the function passing a List(Of C), that
means, a List(Of C) is NOT derived from a List(Of IMyInterface) just because
C implements IMyInterface.

Yes, that makes sense, thank you. :)

Maurizio
 

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