开发者

Initializing a Collection Class

Is there a way to have a class collection of inherited types be initia开发者_开发百科lized?

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?


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


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


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜