views:

74

answers:

3

Is there a way to have a class collection of inherited types be initialized?

For example, here is my code:

Public Class CamryCar
    Property Name As String = "Camry"
    Property Color As String
End Class

Public Class RedCamry
    Inherits CamryCar
    Sub New()
        MyBase.New()
        Color = "Red"
    End Sub
End Class

Public Class BlueCamry
    Inherits CamryCar
    Sub New()
        MyBase.New()
        Color = "Blue"
    End Sub
End Class

What I'm doing today for a colllection is:

Public Class Camrys
    Property Cars As New List(Of CamryCar) From {New RedCamry, New BlueCamry}
End Class

But this gives me an extra property of Cars.

I can also do this:

Public Class Camrys
    Inherits List(Of CamryCar)
End Class

I prefer this one as I don't have an extra property to deal with. But I can find a way to initialize that that list with objects of RedCamry and BlueCamry.

Is it impossible or is there another way to do this?

+1  A: 

It seems like you're looking for a factory style function.

Module Factory 

  Public Function CreateCamrysList As List(of CamryCar) 
    Return New List(Of CamryCar) From {New RedCamry, New BlueCamry}
  End Function

End Module

Now you can use the function CreateCamrysList wherever you need the list of CamryCar objects.

Note: Deriving from List(Of T) is in general a bad solution. If you do need to derive it's better to choose Collection(Of T).

JaredPar
Out of curiosity, why is deriving from `List(Of T)` a bad solution?
WinnerWinnerChickenDinner
+1  A: 

You need to initialize it in a constructor:

Public Class Camrys
    Inherits List(Of CamryCar)

    Public Sub New()
        MyBase.New()
        Add(New RedCamry())
        Add(New BlueCamry())
    End Sub

End Class
awe
+2  A: 

Just another option. I'd probably pass the list in to the constructor. I also added an additional Sub New() that will initialize the collection via reflection.

Imports System.Reflection

Module Module1

    Sub Main()
        Dim camrys As New Camrys

        For Each camry As CamryCar In camrys
            Console.WriteLine(camry.Color)
        Next

        Console.ReadKey()
    End Sub

End Module

Public Class Car

End Class

Public Class CamryCar
    Inherits Car
    Property Name As String = "Camry"
    Property Color As String
End Class

Public Class RedCamry
    Inherits CamryCar
    Sub New()
        MyBase.New()
        Color = "Red"
    End Sub
End Class

Public Class BlueCamry
    Inherits CamryCar
    Sub New()
        MyBase.New()
        Color = "Blue"
    End Sub
End Class

Public Class Camrys
    Inherits List(Of CamryCar)

    Public Sub New(ByVal Camrys As List(Of CamryCar))
        MyBase.New()

        For Each Camry As CamryCar In Camrys
            Add(Camry)
        Next
    End Sub

    Public Sub New()
        MyBase.New()
        InitializeCamrys()
    End Sub

    Public Sub InitializeCamrys()
        Dim asm As Assembly = Assembly.GetExecutingAssembly()

        For Each t As Type In asm.GetTypes()
            If GetType(CamryCar) Is t.BaseType Then
                Dim camry As CamryCar = Activator.CreateInstance(t)
                Add(camry)
            End If
        Next
    End Sub

End Class
brad.huffman
+1 for automagically adding all versions of CamryCar using reflection.
awe
Thanks. I thought it was clever. :)
brad.huffman
Brilliant! Thx.
WinnerWinnerChickenDinner
Cool! My first bounty! Glad you like it.
brad.huffman