Answered by:
Want to eliminate one level in object hierarchy

Question
-
User646364117 posted
Not sure how to do this
I have a hierarchy:
Deals at the top
Deals is a collection of Deal
Deal is a collecton of DealSlots
The way I have implemented this (code shown below) allows me to reference
as
Dim alldeals As New Deals alldeals.AddDeal("DealA") alldeals.Deal(1).DealSlots.DealSlot(1).SlotType = Slots.BuyersAgent
I believe I have an extra 'level' in my hierarchy - so would like to reference as:
alldeals.Deal(1).DealSlot(1).SlotType = Slots.BuyersAgent
I need some guidance as to how to eliminate one level (DealSlots)
My code is shown below
Public Class Deals Private mcolDeals As Collection Private mCount As Integer Public ReadOnly Property Count() As Integer Get Return mCount End Get End Property Public Sub New() mcolDeals = New Collection End Sub Public Sub AddDeal(ByVal vDealName As String) Dim d As New Deal mCount = mCount + 1 d.Name = vDealName mcolDeals.Add(d, d.Name) d = Nothing End Sub Public Function Deal(ByVal vIndex As VariantType) As Deal Deal = mcolDeals.Item(vIndex) End Function Public Sub RemoveDeal() 'do later End Sub End Class '------------------------------------------------------------- Imports Microsoft.VisualBasic Public Class Deal Private mDealSlots As DealSlots Private mDealID As Integer Private mName As String Public Sub New() mDealSlots = New DealSlots End Sub Public Property Name() As String Get Return mName End Get Set(ByVal value As String) mName = value End Set End Property Public Property DealID() As Integer Get Return mDealID End Get Set(ByVal value As Integer) mDealID = value End Set End Property Public Property DealSlots() As DealSlots Get DealSlots = mDealSlots End Get Set(ByVal value As DealSlots) mDealSlots = value End Set End Property ' End Class '---------------------------------------------------------------------- Imports Microsoft.VisualBasic Public Class DealSlots Private mcolDealSlots As Collection Private mCount As Integer Public ReadOnly Property Count() As Integer Get Return mCount End Get End Property Public Sub New() mcolDealSlots = New Collection End Sub Public Sub AddDealSlot(ByVal vDealSlotName As String) Dim ds As New DealSlot mCount = mCount + 1 ds.Name = vDealSlotName mcolDealSlots.Add(ds, ds.Name) ds = Nothing End Sub Public Function DealSlot(ByVal vIndex As VariantType) As DealSlot DealSlot = mcolDealSlots.Item(vIndex) End Function Public Sub RemoveDealSlot() 'do later End Sub End Class '------------------------------------------------------------------------ Imports Microsoft.VisualBasic Public Enum Slots NotDefined = 0 BuyersAgent SellersAgent End Enum Public Class DealSlot Private mSlotType As Slots = Slots.NotDefined Private mName As String Private mPersonKey As Integer Public Property SlotType() As Slots Get Return mSlotType End Get Set(ByVal value As Slots) mSlotType = value End Set End Property Public Property Name() As String Get Return mName End Get Set(ByVal value As String) mName = value End Set End Property Public Property PersonKey() As Integer Get Return mPersonKey End Get Set(ByVal value As Integer) mPersonKey = value End Set End Property ' End Class
Tuesday, May 4, 2010 10:47 PM
Answers
-
User197322208 posted
Put in DealSlots
Default Public Property Item(ByVal Index As Integer) As DealSlot
Get
Return CType(mcolDealSlots .Item(Index), DealSlot)
End Get
Set(ByVal Value As DealSlot)
mcolDealSlots .Item(Index) = Value
End Set
End PropertyAnd you can write:
alldeals.Deal(1).DealSlots(1).SlotType
But why re-invent the wheel ?
Remove the reference of mcolDealSlots from DealsSlots and the functions to manage mcolDeals and use
- Public Class DealSlots Inherits List(Of Deals)
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, May 5, 2010 12:05 AM
All replies
-
User197322208 posted
Put in DealSlots
Default Public Property Item(ByVal Index As Integer) As DealSlot
Get
Return CType(mcolDealSlots .Item(Index), DealSlot)
End Get
Set(ByVal Value As DealSlot)
mcolDealSlots .Item(Index) = Value
End Set
End PropertyAnd you can write:
alldeals.Deal(1).DealSlots(1).SlotType
But why re-invent the wheel ?
Remove the reference of mcolDealSlots from DealsSlots and the functions to manage mcolDeals and use
- Public Class DealSlots Inherits List(Of Deals)
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, May 5, 2010 12:05 AM -
User646364117 posted
I ended up doing code below before I read your replies.
I like your solutions better though
Thank you
Public Class Deal Private mcolDeal As Collection Private mDealID As Integer Private mName As String Private mCount As Integer Public Sub New() mcolDeal = New Collection End Sub Public Sub AddDealSlot(ByVal vDealSlotName As String) Dim ds As New DealSlot mCount = mCount + 1 ds.Name = vDealSlotName mcolDeal.Add(ds, ds.Name) ds = Nothing End Sub Public Function DealSlot(ByVal vIndex As VariantType) As DealSlot DealSlot = mcolDeal.Item(vIndex) End Function ' End Class
Wednesday, May 5, 2010 7:46 AM