473,394 Members | 1,759 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Object models in .NET

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/de...jectmodels.asp

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

Many thanks.
Nov 19 '05 #1
2 2837
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

"onedaywhen" <on********@fmail.co.uk> wrote in message
news:b8**************************@posting.google.c om...
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/de...jectmodels.asp
I'd really like to see a .NET equivalent. I've searched on "Bicycle
Wheel Spoke", I've tried everything <g>.

Many thanks.


Nov 19 '05 #2
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.

"Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> wrote in message news:<ep**************@TK2MSFTNGP12.phx.gbl>...
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

"onedaywhen" <on********@fmail.co.uk> wrote in message
news:b8**************************@posting.google.c om...
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/de...jectmodels.asp

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

Many thanks.

Nov 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

34
by: yensao | last post by:
Hi, I have a hard time to understand difference and similarities between Relational database model and the Object-Oriented model. Can somebody help me with this? Thank you in advance. ...
16
by: sneill | last post by:
How is it possible to take the value of a variable (in this case, MODE_CREATE, MODE_UPDATE, etc) and use that as an object property name? In the following example I want 'oIcon' object to have...
3
by: Steven Quail | last post by:
Hi to all, Having come from a delphi environment, I remember that when using interfaces, we were told not to mix the object models and interface models. In other words, when I create an object,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.