0

I have two objects of BindingSource and Dictionary. And BindingSource Object contains the Object of List of Vehicles Class Object and Dictionary Object contains the BindingSource Class Object. And here is the global declaration:

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

and when user click button named "Add" I add object of Vehicle Class to bs and then add bs to 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)

when I want to retrieve the BindingSource Object from dicBinding Object:

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

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

But I can't retrieve. Could anyone help me?

Eric
  • 248
  • 2
  • 8
  • 21

1 Answers1

0

Now it makes more sense :) Dictionary objects can't be indexed like arrays - they have no inbuilt order like Arrays and Lists do.

You have a Dictionary whose keys are integers and whose values are BindingSources.

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

adds one element to your Dictionary whose value is bs and whose key is 1 (assuming the Dictionary had no entries previously).

When you request dicBinding.Item(0), you're asking for the value whose key is 0, but you haven't added one yet.

prprcupofcoffee
  • 2,950
  • 16
  • 20