Problem with type conversion/inference

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Maurizio Colucci

    Problem with type conversion/inference

    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
  • Armin Zingler

    #2
    Re: Problem with type conversion/inference

    "Maurizio Colucci" <maurizio.coluc ci@gmail.comsch rieb
    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

    Comment

    • Maurizio Colucci

      #3
      Re: Problem with type conversion/inference

      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

      Comment

      Working...