Collection?

  • Thread starter Thread starter nime
  • Start date Start date
N

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"
 
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)
 
nime said:
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.
 
Thank you for all replies, I think I can use SortedList and Structure



Phill W. said:
nime said:
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.
 
Back
Top