开发者

VB.NET : Arraylist of Objects, Listbox1_SelectedIndexChanged, and Linking Values

开发者 https://www.devze.com 2023-01-23 15:32 出处:网络
I\'m completely new to VB.NET and this is likely a few steps over my head, but have managed to get myself this far with just Google.

I'm completely new to VB.NET and this is likely a few steps over my head, but have managed to get myself this far with just Google.

I'm reading an XML file and creating an arraylist of classes for the values in there, as seen:

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 P开发者_Go百科roperty

    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

and...

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

Thus, the arraylist "yresults" is populed with classes "yresults", which has values of strdate, strlink, etc. etc.

How can I iterate through this array list, and compare yresults "strSubject" field to listbox1.selectedvalue? After learning how to do this, I should be able to also scan through the array and get other data I need (particularly strLink).

The code I have now works, but i'm unsure how to proceed from here. I'd appreciate a gentle push (or shove) in the right direction.


Some things that will help you in .Net:

  • Prefer Class over Structure unless you have a good reason not to
  • Don't use ArrayLists - Prefer System.Collections.Generic.List(Of T)
  • Look at using the System.Xml.Serialization.XmlSerializer class to read your xml data in.
0

精彩评论

暂无评论...
验证码 换一张
取 消