VB.NET:对象的Arraylist,Listbox1_SelectedIndexChanged和链接值

时间:2010-11-06 17:46:52

标签: vb.net class arraylist binding

我对VB.NET完全不熟悉,这可能只是我的一小步,但只是谷歌已经设法让自己走得这么远。

我正在阅读一个XML文件并为其中的值创建类的arraylist,如下所示:

Public Class yresults
    'Dim xdate As XmlNodeList = xDoc.GetElementsByTagName("Date")
    'Dim link As XmlNodeList = xDoc.GetElementsByTagName("Link")
    'Dim category As XmlNodeList = xDoc.GetElementsByTagName("Category")
    'Dim UserNick As XmlNodeList = xDoc.GetElementsByTagName("UserNick")
    'Dim numAnswers As XmlNodeList = xDoc.GetElementsByTagName("NumAnswers")
    'Dim numComments As XmlNodeList = xDoc.GetElementsByTagName("NumComments")
    'Dim subject As XmlNodeList = xDoc.GetElementsByTagName("Subject")
    'Dim content As XmlNodeList = xDoc.GetElementsByTagName("Content")

    Private mystrDate As String
    Private mystrLink As String
    Private mystrCat As String
    Private mystrUser As String
    Private mystrNA As String
    Private mystrNC As String
    Private mystrSubject As String
    Private mystrContent As String


    Public Sub New(ByVal strDate As String, ByVal strLink As String, ByVal strCat As String, ByVal strUser As String, ByVal strNA As String, ByVal strNC As String, ByVal strSubject As String, ByVal strContent As String)
        MyBase.New()
        Me.mystrDate = strDate
        Me.mystrLink = strLink
        Me.mystrCat = strCat
        Me.mystrUser = strUser
        Me.mystrNC = strNC
        Me.mystrNA = strNA
        Me.mystrSubject = strSubject
        Me.mystrContent = strContent

    End Sub

    Public ReadOnly Property strDate() As String
        Get
            Return mystrDate
        End Get
    End Property

    Public ReadOnly Property strLink() As String
        Get
            Return mystrLink
        End Get
    End Property

    Public ReadOnly Property strUser() As String
        Get
            Return mystrUser
        End Get
    End Property

    Public ReadOnly Property strNA() As String
        Get
            Return mystrNA
        End Get
    End Property

    Public ReadOnly Property strNC() As String
        Get
            Return mystrNC
        End Get
    End Property

    Public ReadOnly Property strSubject() As String
        Get
            Return mystrSubject
        End Get
    End Property

    Public ReadOnly Property strContent() As String
        Get
            Return mystrContent
        End Get
    End Property

    Public Structure arc
        Public strDate As String
        Public strLink As String
        Public strCat As String
        Public strUser As String
        Public strNA As String
        Public strNC As String
        Public strSubject As String
        Public strContent As String
    End Structure

End Class

和...

Dim cntr As Integer = 0

While cntr < link.Count
    yresults.Add(New Yresults(xdate(cntr).InnerText.ToString, link(cntr).InnerText.ToString, category(cntr).InnerText.ToString, UserNick(cntr).InnerText.ToString, numAnswers(cntr).InnerText.ToString, numComments(cntr).InnerText.ToString, subject(cntr).InnerText.ToString, content(cntr).InnerText.ToString))
    cntr += 1
End While

因此,arraylist“yresults”中包含了“yresults”类,它具有strdate,strlink等值。

如何遍历此数组列表,并将yresults“strSubject”字段与listbox1.selectedvalue进行比较?在学习了如何做到这一点后,我应该能够扫描数组并获得我需要的其他数据(特别是strLink)。

我现在的代码有效,但我不确定如何从这里开始。我很欣赏在正确的方向上轻轻推动(或推)。

1 个答案:

答案 0 :(得分:0)

有些东西可以帮助你.Net:

  • 除非您有充分的理由不
  • ,否则请选择Class over Class
  • 不要使用ArrayLists - 首选System.Collections.Generic.List(Of T)
  • 使用System.Xml.Serialization.XmlSerializer类来读取您的xml数据。