Click here to Skip to main content
15,890,897 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How do I add and retrieve a structure type object to/from a list. Only the last value written seems to be retrieveable. Partial sample in VS2005. Experimental code which only retrieves the last value written.
VB
While (Not datainfile.EndOfStream And i < pk_size)
 With pk_set
  .profile = datainfile.ReadLine '.profile = code(datainfile.ReadLine, myPWD)
  .UID = datainfile.ReadLine '.UID = code(datainfile.ReadLine, myPWD)
  .PWD = datainfile.ReadLine '.PWD = code(datainfile.ReadLine, myPWD)
  .URL = datainfile.ReadLine '.URL = code(datainfile.ReadLine, myPWD)
  j = lstprofiles.Items.Add(.profile) 'profileIdx.Add(.profile) <-- not needed?
  End With
  pkList.Add(pk_set) '<-----<<this increment isn't occurring
  With pkList(i)
  MsgBox("wrote to pkList: index = " & j & ", " & _
                           "count = " & pkList.Count & ", " & _
                            .profile & ", " & .UID & ", " & .PWD & ", " & .URL)
 End With

 ''''With lstprofiles.Items(j) 'was first i-1 then i
 '''' Dim lItemToString As String = .ToString
 '''' MsgBox("read from lstprofiles: index = " & j & ", " & _
 ''''                            lItemToString & " AKA " & lstprofiles.Items(j))
 ''''End With
 i += 1
End While

For i = 0 To 8 'This iterated
 ''''With lstprofiles.Items(i) 'was i-1
 '''' Dim lItemToString As String = .ToString
 '''' MsgBox("read from lstProfiles: position index = " & i & ", |" & lItemToString & "|")
 ''''End With
 ''''MsgBox("lstProfiles.items(" & i & ") = " & lstprofiles.Items(i))
'With pkList(i) ''With pkList.Item(i + 1)
 MsgBox("pkList(" & i & ") = " & pkList(i).ToString & _
        " Profile = " & pkList(i).profile & _
        " UID = " & pkList(i).UID & _
        " PWD = " & pkList(i).PWD & _
        " URL = " & pkList(i).URL)
'End With

Next



For Each pkpr_set In pkList 'this contains the object's last value only,
                            'for all values of each
 With pkpr_set
 MsgBox("pkpr's profile is " & .profile & " and it contains " _
                      & .UID & ", " _
                      & .PWD & ", " _
                      & .URL)
 End With
Next
Posted
Updated 21-Apr-10 5:42am
v2

1 solution

The pk_set variable is always the same object. You are just changing its properties with each iteration. So when the pkList.Add(pk_set) line is executed it is just adding another reference to the same object to the list. To fix this, add a new line before the first With pk_set that looks something like this:

pk_set = New <whatever type pk_set is>()
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900