Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ArrayList of Dictionary objects. Cannot find keys.

Status
Not open for further replies.

TheBugSlayer

Programmer
Sep 22, 2002
887
US
I have something like this in my code:
Code:
...
Private alGPMSPractices As New ArrayList
...
Do

                    'Load the ListView with DataReader rows
                    For intlvColumnIndex = 0 To   lvGPMSPractices.Columns.Count - 1
                        intColumnOrdinal = dr.GetOrdinal(lvGPMSPractices.Columns.Item(intlvColumnIndex).Tag)
                        'Add a future listview column's value
                        strSubItems(intlvColumnIndex) = _
                           IIf(IsDBNull(dr.Item(intColumnOrdinal)), String.Empty, dr.Item(intColumnOrdinal))
                        'Add to the the dictionary
                        [COLOR=red]d.Add(lvGPMSPractices.Columns.Item(intlvColumnIndex).Tag, strSubItems(intlvColumnIndex))[/color]
                    Next
                    lvItem = New ListViewItem(strSubItems)
                    lvGPMSPractices.Items.Add(lvItem)
[COLOR=red]
                    alGPMSPractices.Add(d)
                    d.Clear()
[/color]
                    
Loop While dr.Read()
    
[\code]
The practices (d Dictionary object) are added to the array list. But once d.Clear() executes, they go away, then when control enters d.Read() loop again, the next one is added and they all show. However, I get an exception stating key does not exist when I try to retrieve a value as in:
[code]
alGPMSPractices.Item(0).Item("Practice_Code")
...
Dim dic As Dictionary(Of String, String)
dic = alGPMSPractices.Item(0).Item("Practice_Description")

Am I accessing the dictionary elements the wrong way? When I try to access the alGPMSPractices member in another method, the same exception is raised as it becomes empty.

If I don't do d.Clear() then the dictionary complains that the key already exists.

Any help is appreciated.
 
without more code, and the fact I dont use dictionary objects for anything, it sounds like you have redeclared the object you are trying to access. So I suggest checking the scope of your variables and objects.
 
Which object is redeclared and where? Well, not in the code that I have not posted here...Tomorrow I will post the other part.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top