如何在列表中添加结构类型对象或从列表中检索结构类型对象? [英] How do I add and retrieve a structure type object to/from a list?

查看:93
本文介绍了如何在列表中添加结构类型对象或从列表中检索结构类型对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在列表中添加结构类型对象或从列表中检索结构类型对象.只有最后写入的值似乎是可检索的. VS2005中的部分样本.实验代码,仅检索最后写入的值.

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.

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

推荐答案

pk_set变量始终是同一对象.您只需在每次迭代中更改其属性.因此,当执行pkList.Add(pk_set)行时,它只是将对同一对象的另一个引用添加到列表中.要解决此问题,请在第一个With pk_set之前添加新行,如下所示:

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>()


这篇关于如何在列表中添加结构类型对象或从列表中检索结构类型对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆