|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
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("John") Student("John").Class = "White" Hello nime,
> How can I do a collection like thing in VB.NET If you're using VS2005 then you could do the following > > Struct StudentStruct > Id as Integer > Name as String > Class as String > End Struct > ... > > Dim Student as StudentStruct > > Student.Add("John") > Student("John").Class = "White" 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. nime wrote:
> How can I do a collection like thing in VB.NET Since Student ID's should be unique, how about a HashTable?> > Struct StudentStruct > Id as Integer > Name as String > Class as String > End Struct > > Dim Student as StudentStruct > Student.Add("John") > Student("John").Class = "White" 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
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.
Form_Keydown being overridden
circular left shift in VB Sending a command to a COM port... Application KeepAlive? MouseHover doesn't work? Can not create 2 properties with same signature. Servlet Counterpart in VB.NET Properties with generics adding a console application Can i read the text in MSword document using Word Object Library? |
|||||||||||||||||||||||