Passing List (Of ChildClass) as parameter to method expecting List (Of ParentClass)?

Posted by Nicholas on Stack Overflow See other posts from Stack Overflow or by Nicholas
Published on 2010-04-12T09:30:35Z Indexed on 2010/04/12 9:33 UTC
Read the original article Hit count: 353

Hi, I have implemented inheritance for two parent classes called Table and Field. Each parent class has several child classes. In the Table parent class, I created a method that expects parameter List(Of Field).

I get an error when trying to pass in parameter List(Of ChildField) to the method that expects a parameter of List(Of Field). The error message is as below:

Value of type 'System.Collections.Generic.List(Of com.hlb.icisbatch.data.ChildField)'
cannot be converted to 'System.Collections.Generic.List(Of com.hlb.icisbatch.data.Field)'

My question, is it possible to pass in list of child class as parameter? If it is not a list then it works fine. But somehow it is not possible with lists?

Below is sample class structure:

    Table              Field
      |                  |
  ChildTable         ChildField

I have a method in the parent class:

Public Class Table

    Public Sub New()
    End Sub

    Public Overridable Sub setFields(ByVal list As List(Of Field)
       'Do work here'
    End Sub

 End Class

and method in child class:

Public Class ChildTable

    Public Sub New(ByVal list As List(Of ChildField)
       setFields(ChildField)
    End Sub

 End Class

© Stack Overflow or respective owner

Related posts about vb.net

Related posts about .net-2.0