Collection?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • nime

    #1

    Collection?

    How can I do a collection like thing in VB.NET

    Struct StudentStruct
    Id as Integer
    Name as String
    Class as String
    End Struct

    ....

    Dim Student as StudentStruct

    Student.Add("Jo hn")
    Student("John") .Class = "White"



  • Jared Parsons [MSFT]

    #2
    Re: Collection?

    Hello nime,
    How can I do a collection like thing in VB.NET
    >
    Struct StudentStruct
    Id as Integer
    Name as String
    Class as String
    End Struct
    ...
    >
    Dim Student as StudentStruct
    >
    Student.Add("Jo hn")
    Student("John") .Class = "White"
    If you're using VS2005 then you could do the following

    Dim student As New List(Of StudentStruct)

    --
    Jared Parsons [MSFT]
    jaredpar@online .microsoft.com
    All opinions are my own. All content is provided "AS IS" with no warranties,
    and confers no rights.


    Comment

    • Phill W.

      #3
      Re: Collection?

      nime wrote:
      How can I do a collection like thing in VB.NET
      >
      Struct StudentStruct
      Id as Integer
      Name as String
      Class as String
      End Struct
      >
      Dim Student as StudentStruct
      Student.Add("Jo hn")
      Student("John") .Class = "White"
      Since Student ID's should be unique, how about a HashTable?

      Dim almuni As New HashTable
      Dim student As StudentStruct

      With student
      .Id = 172
      .Name = "John"
      .Class = "VB 101"
      End With
      alumni.Add( student )

      However, I'd suggest using a Class instead of a Structure, though. That
      way, you can encapsulate [all] the property setting in a single
      statement, as in

      Class Student

      Friend Sub New( _
      ByVal iId as Integer _
      , ByVal sName as String _
      )
      m_iId = iId
      m_sName = sName
      End Sub

      Public ReadOnly Property Id() As Integer
      Get
      Return m_iId
      End Get
      End Property

      Public ReadOnly Property Name() As String
      Get
      Return m_sName
      End Get
      End Property

      Private m_iId as Integer = -1
      Private m_sName as String = Nothing

      End Class

      .... then ...

      Then

      Dim almuni As New HashTable

      alumni.Add( New Student( 172, "John" ) )

      Or, you could create a constructor (Sub New) that took, say, a DataRow
      and populated a Student object from that ...

      HTH,
      Phill W.

      Comment

      • nime

        #4
        Re: Collection?

        Thank you for all replies, I think I can use SortedList and Structure



        "Phill W." <p-.-a-.-w-a-r-d@o-p-e-n-.-a-c-.-u-kwrote in message news:e92u5c$hpr $1@yarrow.open. ac.uk...
        nime wrote:
        >How can I do a collection like thing in VB.NET
        >>
        >Struct StudentStruct
        > Id as Integer
        > Name as String
        > Class as String
        >End Struct
        >>
        >Dim Student as StudentStruct
        >Student.Add("J ohn")
        >Student("John" ).Class = "White"
        >
        Since Student ID's should be unique, how about a HashTable?
        >
        Dim almuni As New HashTable
        Dim student As StudentStruct
        >
        With student
        .Id = 172
        .Name = "John"
        .Class = "VB 101"
        End With
        alumni.Add( student )
        >
        However, I'd suggest using a Class instead of a Structure, though. That way, you can encapsulate [all] the property setting in a
        single statement, as in
        >
        Class Student
        >
        Friend Sub New( _
        ByVal iId as Integer _
        , ByVal sName as String _
        )
        m_iId = iId
        m_sName = sName
        End Sub
        >
        Public ReadOnly Property Id() As Integer
        Get
        Return m_iId
        End Get
        End Property
        >
        Public ReadOnly Property Name() As String
        Get
        Return m_sName
        End Get
        End Property
        >
        Private m_iId as Integer = -1
        Private m_sName as String = Nothing
        >
        End Class
        >
        ... then ...
        >
        Then
        >
        Dim almuni As New HashTable
        >
        alumni.Add( New Student( 172, "John" ) )
        >
        Or, you could create a constructor (Sub New) that took, say, a DataRow and populated a Student object from that ...
        >
        HTH,
        Phill W.

        Comment

        Working...