如何遍历VB.net字典集合

时间:2016-02-21 04:09:43

标签: vb.net

当我遍历这个集合时,他们键和省不同步。有人可以告诉我为什么吗?

Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
    Dim Country As Dictionary(Of String, Province)

    Country = Module1.CreateCountry

    Dim s As String

    s = ""

    For Each kvp As KeyValuePair(Of String, Province) In Country
        s = s + kvp.Key + "; " + kvp.Value.ProvName + "; " + kvp.Value.Region + "; " + kvp.Value.Population + vbCrLf
    Next

    TextBox4.Text = s

End Sub

模块模块1

Module Module1

    Public provinces As CollectionBase


    Function CreateCountry() As Dictionary(Of String, Province)

        Dim Country As New Dictionary(Of String, Province)

        Dim Prov As Province
        Prov = New Province

        With Prov
            .Abbrv = "Qc"
            .ProvName = "Quebec"
            .Population = "7 500 000"
            .Region = "East"
        End With
        Country.Add(Prov.Abbrv, Prov)


        With Prov
            .Abbrv = "BC"
            .ProvName = "British Columbia"
            .Population = "4 500 000"
            .Region = "West"
        End With
        Country.Add(Prov.Abbrv, Prov)

        With Prov
            .Abbrv = "AB"
           .ProvName = "Alberta"
            .Population = "5 500 000"
            .Region = "Prairies"
        End With
        Country.Add(Prov.Abbrv, Prov)

        With Prov
            .Abbrv = "NS"
            .ProvName = "Nova Scotia"
            .Population = "2 000 000"
            .Region = "Maritimes"
        End With
        Country.Add(Prov.Abbrv, Prov)

        Return Country

    End Function
End Module

Public Class Province

Public Class Province

    Public Property Abbrv As String
    Public Property ProvName As String
    Public Property Population As String
    Public Property Region As String

End Class

3 个答案:

答案 0 :(得分:1)

因为在词典中添加了Province的指针引用。

在您的情况下,您不会创建Province的新实例,但每次更改同一实例的属性时。因此,当您完成时,Province的实例会保留属性的最后一个值。

对于每次添加,请创建Country.Add新实例。

答案 1 :(得分:1)

试试这个......

Module Module1

Sub Main()
    ' use a List instead of a Dictionary
    Dim Country As New List(Of Province)

    ' this code adds 4 Province objects to  Country List
    Country.Add(New Province() With {.Abbrv = "Qc", .Population = "7500000", .ProvName = "Quebec", .Region = "East"})
    Country.Add(New Province() With {.Abbrv = "BC", .Population = "4500000", .ProvName = "British Columbia", .Region = "West"})
    Country.Add(New Province() With {.Abbrv = "AB", .Population = "5500000", .ProvName = "Alberta", .Region = "Prairies"})
    Country.Add(New Province() With {.Abbrv = "NS", .Population = "2000000", .ProvName = "Nova Scotia", .Region = "Maritimes"})

    ' now you can select any one of the 4 elements in the  Country List like so....
    Dim prov As Province = (From c In Country Where c.Abbrv = "AB" Select c).FirstOrDefault

End Sub

End Module

Public Class Province

Public Property Abbrv As String
Public Property ProvName As String
Public Property Population As String
Public Property Region As String

End Class

答案 2 :(得分:0)

您需要掌握引用类型才能理解并解决问题。

简而言之,您希望词典中的四个不同Province个实例,所有实例都具有不同的属性值 - 但您只创建一个(Prov = New Province),然后继续改变同一个实例并将其重新添加到字典中。当然,您最终得到的是一个包含4 KeyValuePair s的字典,其中Value都指向同一个Province实例。

解决方案是New向上Province实例,然后再设置其属性值并将其添加到字典中,如下所示:

Function CreateCountry() As Dictionary(Of String, Province)

    Dim Country As New Dictionary(Of String, Province)

    Dim Prov As Province
    Prov = New Province ' Prov now points to instance #1.

    With Prov
        .Abbrv = "Qc"
        .ProvName = "Quebec"
        .Population = "7 500 000"
        .Region = "East"
    End With
    Country.Add(Prov.Abbrv, Prov)

    Prov = New Province ' Added. Prov now points to instance #2.

    With Prov
        .Abbrv = "BC"
        .ProvName = "British Columbia"
        .Population = "4 500 000"
        .Region = "West"
    End With
    Country.Add(Prov.Abbrv, Prov)

    Prov = New Province ' Added. Prov now points to instance #3.

    With Prov
        .Abbrv = "AB"
       .ProvName = "Alberta"
        .Population = "5 500 000"
        .Region = "Prairies"
    End With
    Country.Add(Prov.Abbrv, Prov)

    ' Alternative syntax. Prov will point to instance #4.
    Prov = New Province With {
        .Abbrv = "NS",
        .ProvName = "Nova Scotia",
        .Population = "2 000 000",
        .Region = "Maritimes"
    }
    Country.Add(Prov.Abbrv, Prov)

    Return Country

End Function

请注意上一个Prov实例的替代初始化语法。