Home All Groups Group Topic Archive Search About
Author
11 Jul 2006 9:10 PM
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("John")
Student("John").Class = "White"

Author
11 Jul 2006 9:34 PM
Jared Parsons [MSFT]
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("John")
> 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]
jared***@online.microsoft.com
All opinions are my own. All content is provided "AS IS" with no warranties,
and confers no rights.
Author
12 Jul 2006 1:44 PM
Phill W.
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("John")
> 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.
Author
13 Jul 2006 1:26 PM
nime
Thank you for all replies, I think I can use SortedList and Structure



Show quoteHide quote
"Phill W." <p-.-a-.-w-a-r-d@o-p-e-n-.-a-c-.-u-k> wrote 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("John")
>> 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.