Object models in .NET

  • Thread starter Thread starter onedaywhen
  • Start date Start date
O

onedaywhen

Does anyone know of a good .NET article, sample, etc to demonstrate
creating an object model, one where you have one externally creatable
object with a hierarchy of dependent objects? For example, the only
way to create a new Spoke object is to first create a new Bicycle
object, add to its Wheel collection then add to its Spoke collection.

Here's a good article for doing this in VB6:

http://msdn.microsoft.com/library/d...propertiescollectionstocreateobjectmodels.asp

I'd really like to see a .NET equivalent. I've searched on "Bicycle
Wheel Spoke", I've tried everything <g>.

Many thanks.
 
onedaywhen,
I'm not sure of an article, however here is the sample loosely (read
quickly) translated into VB.NET. Notice the Friend vs. Public on the Sub
New. Sub New is called the constructor, its used in the creation of a new
object. Notice on the Spoke constructor, the PartNumber & Allow are passed
as parameters to the constuctor.

All the classes are public, which means they can be used outside of the
assembly. Some of the constructors are Friend, which means that only the
containing assembly can create one of those objects. Some of the
constructors are public, which means that referencing assemblies can create
those objects.

A public method can be used by assemblies outside of this assembly, a Friend
method can only be used by other classes within this assembly.

The problem with this object model, is you cannot buy a new frame for your
bicycle or new wheels, or new spokes. The Readonly on the fields ensure that
the instance variable will not be modified after the object has been
constructed, the constructor can set the field, then its 'frozen'.

Public Class Bicycle

Private ReadOnly m_frame As Frame

Public Sub New()
m_frame = New Frame
End Sub

Public ReadOnly Property Frame() As Frame
Get
Return m_frame
End Get
End Property

End Class

Public Class Frame

Private ReadOnly m_frontWheel As Wheel
Private ReadOnly m_rearWheel As Wheel

Friend Sub New()
m_frontWheel = New Wheel
m_rearWheel = New Wheel
End Sub

Public ReadOnly Property Frontwheel() As Wheel
Get
Return m_frontWheel
End Get
End Property

Public ReadOnly Property Rearwheel() As Wheel
Get
Return m_rearWheel
End Get
End Property

End Class

Public Class Wheel

Private ReadOnly m_spokes As SpokeCollection

Public Sub New()
m_spokes = New SpokeCollection
End Sub

Public ReadOnly Property Spokes() As SpokeCollection
Get
Return m_spokes
End Get
End Property

End Class

Public Class SpokeCollection
Inherits CollectionBase

Friend Sub New()

End Sub

Public Function Add(ByVal partNumber As Integer, _
ByVal allow As Integer) As Spoke
Dim spoke As New Spoke(partNumber, allow)
InnerList.Add(spoke)
Return spoke
End Function

Default Public ReadOnly Property Item(ByVal index As Integer) _
As Spoke
Get
Return DirectCast(innerlist(index), Spoke)
End Get
End Property

End Class

Public Class Spoke

Private m_partNumber As Integer
Private m_allow As Integer

Friend Sub New(ByVal partNumber As Integer, ByVal allow As Integer)
m_partNumber = partNumber
m_allow = allow
End Sub


Public Readonly Property PartNumber() As Integer
Get
Return m_partNumber
End Get
End Property

Public Readonly Property Allow() As Integer
Get
Return m_allow
End Get
End Property

Public Sub Adjust()

End Sub

End Class

Hope this helps
Jay
 
Jay, Thank you for taking the time to put that object model together.
I knew some of the new tricks (inheriting from CollectionBase, using a
Friend constructor in a Public class) but I admit I was having trouble
putting it together myself.

Once again, many thanks for setting me back on track.
 
Back
Top