如何从Dictionary中检索BindingSource的对象

时间:2012-11-11 04:21:32

标签: vb.net dictionary bindingsource

我有两个BindingSource和Dictionary对象。并且BindingSource对象包含车辆列表对象的对象,而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

但是我找不到。有谁可以帮助我?

1 个答案:

答案 0 :(得分:0)

现在它更有意义:) Dictionary对象不能像数组一样被编入索引 - 它们没有像ArrayList这样的内置命令。

您有一个Dictionary,其键是整数,其值为BindingSource s。

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

为您的Dictionary添加一个元素,其值为bs且其键为1(假设Dictionary之前没有条目)。

当您请求dicBinding.Item(0)时,您要求的是密钥为0的值,但尚未添加。{/ p>