0

我有 BindingSource 和 Dictionary 两个对象。BindingSource 对象包含List of Vehicles 类对象的对象,Dictionary 对象包含BindingSource 类对象。这是全局声明:

        Dim bs As BindingSource
        Dim dicBinding As Dictionary(Of Integer,BindingSource)

当用户单击名为“添加”的按钮时,我将车辆类的对象添加到 bs,然后将 bs 添加到 dicBinding:

        If bs Is Nothing Then bs = New BindingSource(New List(Of Vehicle),Nothing)
        If dicBinding is Nothing Then dicBinding = New Dictionary(Of integer,BindingSource)
        Dim i As Integer = dicBinding.Count
        dicBinding.Add(i,bs)

当我想从 dicBinding 对象中检索 BindingSource 对象时:

        bs =TryCast(dicBinding.Item(0),BindingSource)

        For Each v As Vehicle In bs.List
            MessageBox.Show(v.VehicleId)
        Next

但我无法找回。有人可以帮我吗?

4

1 回答 1

0

现在它更有意义了 :)Dictionary对象不能像数组一样被索引——它们没有像Arrays 和Lists 那样的内置顺序。

你有一个Dictionary,它的键是整数,它的值是BindingSources。

Dim i As Integer = dicBinding.Count + 1
dicBinding.Add(i, bs)

将一个元素添加到您Dictionary的值bs和键是1(假设Dictionary之前没有条目)。

当您请求时dicBinding.Item(0),您要求的是键为 的值0,但您尚未添加一个。

于 2012-11-11T04:38:15.950 回答