Monday, March 26, 2012

OO Relationship modelling in .NET

Hi,

Dipping my toe into OO stuff here.

SoI've designed a simple application in UML. It has classes (obviously)in various one-to-one/one-to-many relationships and association classesto sort out the many-to-many relationships. Looks good on paper.

However,I'm getting myself into a dreadful tangle trying to actually representthis in code. It's the relationship modelling that's really getting meand I can't find any code samples. All I've got to go on so far is theadvice to "Use a reference variable as an attribute of a class" andthat "A reference variable points to an actual instance". But being apractical guy, I'd like to see a practical example and I can't findany.

Can anyone outline how this is supposed to work, orpoint me at a tutorial with actual code? VB in preference but I cantranslate C# if I have to.

Cheers,


'''

' Employe (n) -- (1) Job
' Company (n) -- (n) Employe

'''

Public Class Job

Private _name As String

Public Property Name() As String
Get
Return _name
End Get
Set(ByVal Value As String)
_name = Value
End Set
End Property

End Class

Public Class Employe

Private _name As String
Private _job As Job

Public Property Name() As String
Get
Return _name
End Get
Set(ByVal Value As String)
_name = Value
End Set
End Property

Public Property Job() As Job
Get
Return _job
End Get
Set(ByVal Value As Job)
_job = Value
End Set
End Property

End Class

Public Class Company

Private _name As String
Private _employes As ArrayList = New ArrayList

Public Property Name() As String
Get
Return _name
End Get
Set(ByVal Value As String)
_name = Value
End Set
End Property

Public Property Employes() As ArrayList
Get
Return _employes
End Get
Set(ByVal Value As ArrayList)
_employes = Value
End Set
End Property

End Class

'''

Dim j As Job
Dim e1, e2 As Employe
Dim c As Company

c = New Company
e1 = New Employe
e2 = New Employe
j = New Job

e1.Job = j
e2.Job = j
c.Employes.Add(e1)
c.Employes.Add(e2)

0 comments:

Post a Comment