howtousecollections

How To Use Collections in Class ?

This is a Sample how to use Collection in Our Class.

For more Details of Collections Read MSDN:

Collection Name Space

BaseCollection Class

Back

'Team Class For Which we will create Collection Of Team

Public Class Team

Implements IDisposable

Private strName As String

Private intID As Integer

Private strCreateDate As Date

Private intOwner As Integer

''' <summary>

''' Name of Team

''' </summary>

Public Property Name() As String

Get

Return strName

End Get

Set(ByVal value As String)

strName = value

End Set

End Property

''' <summary>

''' ID of Team

''' </summary>

Public Property ID() As Integer

Get

Return intID

End Get

Set(ByVal value As Integer)

intID = value

fillTeam()

End Set

End Property

''' <summary>

''' Creation Date

''' </summary>

Public Property CreatedDate() As Date

Get

Return strCreateDate

End Get

Set(ByVal value As Date)

strCreateDate = value

End Set

End Property

''' <summary>

''' Owner of this Team

''' </summary>

Public Property OwnerID() As Integer

Get

Return intOwner

End Get

Set(ByVal value As Integer)

intOwner = value

End Set

End Property

Public Function addTeam() As Boolean

Try

' Insert the team details in DataBase here

Catch ex As Exception

Throw ex

Return False

End Try

Return True

End Function

Public Function updateTeam() As Boolean

Try

'Update Team details here in database

Catch ex As Exception

Throw ex

Return False

End Try

End Function

Public Function DeleteTeam(ByVal empID As Integer) As Boolean

Try

'Delete Team from DataBAse

Return True

Catch ex As Exception

Throw ex

Return False

End Try

End Function

Private Sub fillTeam()

'get the team details and assign to private fields

End Sub

' To detect redundant calls

Private disposedValue As Boolean = False

' IDisposable

Protected Overridable Sub Dispose(ByVal disposing As Boolean)

On Error Resume Next

If Not Me.disposedValue Then

If disposing Then

'Dispose the team details

End If

' TODO: free shared unmanaged resources

End If

Me.disposedValue = True

End Sub

#Region " IDisposable Support "

' This code added by Visual Basic to correctly implement the disposable pattern.

Public Sub Dispose() Implements IDisposable.Dispose

' Do not change this code. Put cleanup code in Dispose(ByVal disposing As Boolean) above.

Dispose(True)

GC.SuppressFinalize(Me)

End Sub

#End Region

Public Sub New()

'Initialise Team Details

End Sub

Public Sub New(ByVal id As Integer)

'Initialise Team Details and set private field

according to id of team

End Sub

End Class

'Team Collection Begins here

Public Class TeamCollection

'Inherit Collection base or you can inherit ICollection

Inherits CollectionBase

Sub New(ByVal capacity As Integer)

MyBase.New(capacity)

End Sub

Sub New()

MyBase.New()

End Sub

Default Public Property Item(ByVal index As Integer) As Team

Get

Return CType(List.Item(index), Team)

End Get

Set(ByVal value As Team)

List.Item(index) = value

End Set

End Property

'Add Team in Collection

Public Function Add(ByVal tim As Team) As Integer

Return List.Add(tim)

End Function

'Insert Team in Collection

Public Sub Insert(ByVal index As Integer, ByVal tim As Team)

List.Insert(index, tim)

End Sub

'Remove Team from Collection

Public Sub Remove(ByVal tim As Team)

List.Remove(tim)

End Sub

'Remove team from Collection

Public Shadows Sub RemoveAt(ByVal index As Integer)

List.RemoveAt(index)

End Sub

End Class

'Class Project which will use Team Collection

'To have multiple teams in Project.

Public Class Project

Private _teams As TeamCollection

Private strName As String

Private intID As Integer

''' <summary>

''' Id of current Project

''' <para>On ID Change it will Reload The Project Information.</para>

''' </summary>

Public Property ID() As Integer

Get

Return intID

End Get

Set(ByVal value As Integer)

intID = value

fillProject()

End Set

End Property

''' <summary>

''' Name of Project

''' </summary>

Public Property Name() As String

Get

Return strName

End Get

Set(ByVal value As String)

strName = value

End Set

End Property

'Teams For Project

Public Property Teams() As TeamCollection

Get

Return _teams

End Get

Set(ByVal value As TeamCollection)

_teams = value

End Set

End Property

'Constructor initialize project here

Public Sub New()

'Teams for Project

_teams = New TeamCollection()

strName = ""

intID = -1

End Sub

End Class

'Demo Class Which will use Class Project

'and add few team in project

Public Class Demo

Dim Prj As Project

Dim Tim1 As new Team

Dim Tim2 As new Team

Dim Tim2 As new Team

'Add Some team in project

Prj.Teams.Add(Tim1)

Prj.Teams.Add(Tim2)

Prj.Teams.Add(Tim3)

.

.

.

End Class