从公共结构和集合中检索数据

时间:2013-10-04 13:50:15

标签: vb.net visual-studio-2010 data-structures collections

我正在使用集合和结构在类中存储一些已解析的数据,但是当我尝试检索数据时,它是null。在form1上,我可以获得响应字符串,这是所有原始数据。我是否正确地将解析后的数据添加到集合中?我是否正确地在form1上调用它?

Private Sub btnStart_Click(sender As System.Object, e As System.EventArgs) Handles btnStart.Click
    Dim dloader as new Downloader(blah)
 'this does not work
    Dim test as new _ListInfo
      msgbox(test.Name) ' produces an empty message box
'this works
     msgbox(dloader.Download)
End Sub

这是我的课程代码:

Public Structure _Info
  Dim Name As String
End Structure

Public Class Downloader
  Dim _ListCollection As New Collection(Of _ListInfo)

Public ReadOnly Property ListCollection() As Collection(Of _ListInfo)
    Get
        ListCollection = _ListCollection
    End Get
End Property

Public Function Download() As String
  'doing the download
  ParseList()
  Return _ResponseString
End Function    

Private Sub ParseList()
    _ListCollection.Clear()
 Dim Name As String
 Dim MyInfo As _ListInfo
   MyInfo.Name = Name
    _ListCollection.Add(MyInfo)
End Sub

1 个答案:

答案 0 :(得分:1)

为什么你期望它能起作用?您只是在新建一个结构并访问一个属性。你不想做这样的事情:

Dim dloader as new Downloader(blah)
dloader.Download()
' Show first name.
MsgBox(dloader.ListCollection(0).Name)