如何自动将多个项目添加到列表中

时间:2014-05-12 19:25:01

标签: .net vb.net list visual-studio-2012

列表:

Public LimoLijst As List(Of Limo)
    Dim _Limo1 As New Limo With {.Merk = "Licoln Limo", .Kleur = "Black"}
    Dim _Limo2 As New Limo With {.Merk = "Licoln Limo", .Kleur = "Pink"}
    Dim _Limo3 As New Limo With {.Merk = "Licoln Limo", .Kleur = "White"}
    Dim _Limo4 As New Limo With {.Merk = "Hummer", .Kleur = "Yellow"}
    Dim _Limo5 As New Limo With {.Merk = "Hummer", .Kleur = "White"}
    Dim _Limo6 As New Limo With {.Merk = "Hummer", .Kleur = "Black"}
    Dim _Limo7 As New Limo With {.Merk = "Chrysler 300C Limo", .Kleur = "White"}
    Dim _Limo8 As New Limo With {.Merk = "Chrysler 300C Limo", .Kleur = "Tuxedo Crème"}
    Dim _Limo9 As New Limo With {.Merk = "Porsche Cayenne Limousine", .Kleur = "White"}
    Dim _Limo10 As New Limo With {.Merk = "Porsche Cayenne Limousine", .Kleur = "Electric Blue"}
    Dim _Limo11 As New Limo With {.Merk = "Mercedes SL 55 AMG", .Kleur = "Silver"}
    Dim _Limo12 As New Limo With {.Merk = "Mercedes SL 55 AMG", .Kleur = "Silver"}
    Dim _Limo13 As New Limo With {.Merk = "Chrysler 300C Sedan", .Kleur = "White"}
    Dim _Limo14 As New Limo With {.Merk = "Chrysler 300C Sedan", .Kleur = "White"}
    Dim _Limo15 As New Limo With {.Merk = "Chrysler 300C Sedan", .Kleur = "Black"}
    Dim _Limo16 As New Limo With {.Merk = "Chrysler 300C Sedan", .Kleur = "Black"}

我想通过在每个_Limo名称变量中添加+1来在for循环中添加它们 这样的东西

Sub New()

    For number As Int32 = 1 To 16

        LimoLijst.Add(trycast("_Limo" + (CType(number, String)), Limo)
    Next
End Sub

出于某种原因,我不能操纵变量名,以便每次都将它添加到列表中。

我希望_Limo1添加到列表中。然后将一个添加到变量编号然后 添加_Limo2 ......等等......有什么想法吗?

感谢名单

1 个答案:

答案 0 :(得分:-1)

好吧,现在我猜...

我改变了这个答案,所以人们可以看到好结果。 而不是首先声明变量..我将它们直接添加到列表中,如此

Public LimoLijst As New List(Of Limo) From {New Limo With {.Merk = "Licoln Limo", .Kleur = "Black"}, _
                                                New Limo With {.Merk = "Licoln Limo", .Kleur = "Pink"}, _
                                                New Limo With {.Merk = "Licoln Limo", .Kleur = "White"}, _
                                               New Limo With {.Merk = "Hummer", .Kleur = "Yellow"}, _
                                                New Limo With {.Merk = "Hummer", .Kleur = "White"},
                                                New Limo With {.Merk = "Hummer", .Kleur = "Black"}, _
                                                New Limo With {.Merk = "Chrysler 300C Limo", .Kleur = "White"}, _
                                                New Limo With {.Merk = "Chrysler 300C Limo", .Kleur = "Tuxedo Crème"}, _
                                                New Limo With {.Merk = "Porsche Cayenne Limousine", .Kleur = "White"}, _
                                                New Limo With {.Merk = "Porsche Cayenne Limousine", .Kleur = "Electric Blue"}, _
                                                New Limo With {.Merk = "Mercedes SL 55 AMG", .Kleur = "Silver"}, _
                                                New Limo With {.Merk = "Mercedes SL 55 AMG", .Kleur = "Silver"}, _
                                                New Limo With {.Merk = "Chrysler 300C Sedan", .Kleur = "White"}, _
                                                New Limo With {.Merk = "Chrysler 300C Sedan", .Kleur = "White"}, _
                                                New Limo With {.Merk = "Chrysler 300C Sedan", .Kleur = "Black"}, _
                                                 New Limo With {.Merk = "Chrysler 300C Sedan", .Kleur = "Black"}}