Click to See Complete Forum and Search --> : Creating a custom collection using 2 classes & 1 structure (VB.NET)


DrEvilBeer
02-11-2004, 01:54 PM
I'm trying to create a custom collection class that will aloow me to implement it in the following fashion...

-
Dim SalesTRX As New SalesTransaction

SalesTRX.LineItems.Add("12345", "Item 12345 - Random Widget")
SalesTRX.LineItems.Add("12346", "Item 12346 - Random Widget #2")

Console.WriteLine(SalesTRX.LineItems(0).ItemNumber) 'Would return "12345" as string
SalesTRX.LineItems(0).ItemNumber = "54321" 'Would change value from 12345 to 54321
-

Below is the code I have so far, but obviously there's something I'm missing... I just couldn't wrap my mind around what to do next.

I have two classes, "SalesTransaction" and "LineItemCollection".

The LineItemCollection class is used in SalesTransaction and LineItem structure objects should be members of the LineItemCollection. LineItem objects should be created when the Add method of LineItemCollection is invoked and should be able to be read and written to from the "Item" property in LineItemCollection. If someone could point me in the right direction it would be of great help to me. Thanks!

--------


Public Class SalesTransaction

Private collectionLineItem As LineItemCollection

Public Property LineItems(ByVal lineNumber As Integer) As LineItemCollection
Get
Return Me.collectionLineItem(lineNumber)
End Get
Set(ByVal Value As LineItemCollection)
Me.collectionLineItem(lineNumber) = Value
End Set
End Property

End Class




Public Class LineItemCollection

Private itemLine As LineItem

Default Public Property Item(ByVal lineNumber As Integer) As LineItem
Get
Return 'Me.itemLine(lineNumber)
End Get
Set(ByVal Value As LineItem)
'Me.itemLine(lineNumber) = Value
End Set
End Property

Sub Add(ByVal itemNumber, ByVal itemName)
'Add line item object to LineItemCollection
End Sub

Sub Remove(ByVal itemNumber)
'Remove item from collection by item number
End Sub

End Class




Public Structure LineItem

Private m_ItemNumber As String
Private m_ItemName As String

Public Property ItemNumber() As String
Get
Return Me.m_ItemNumber
End Get
Set(ByVal Value As String)
m_ItemNumber = Value
End Set
End Property

Public Property ItemName() As String
Get
Return Me.m_ItemName
End Get
Set(ByVal Value As String)
m_ItemName = Value
End Set
End Property

End Structure

DrEvilBeer
02-11-2004, 02:46 PM
Perhaps someone can point me to an article that might shed some light on this? Or perhaps there's a book that covers this somewhere?

Raul
02-12-2004, 09:03 PM
These articles might help

Grouping Data in Collections (http://msdn.microsoft.com/library/en-us/cpguide/html/cpcongroupingdataincollections.asp?frame=true)

Collections in .NET (http://www.microsoft.com/belux/fr/msdn/community/columns/jtielens/collections1.mspx)

Create .NET Custom Collections (http://www.fawcette.com/vsm/2002_10/magazine/features/balena/)