HowTo get property of PARENT custom class instance from a CHILâ€D


F

FlashMerlot

How to access a property of the PARENT custom class instance from a CHILD
custom class instance.

Simply as a convenience for discussion ... let's assume

A Mammal has teeth
Teeth are an array of Tooth's
From inside one instance of the TOOTH class ...
... how do I get access to a property (Carnivore)
in the MAMMAL class?

'-------------------------------------------------------------
Public Class Mammal
Private myTeeth As New Teeth()
Public Sub New()
End Sub
Private pCarnivore As Boolean = False
Public Property Carnivore() As Boolean
Get
Return pCarnivore
End Get
Set(ByVal value As Boolean)
pCarnivore = value
End Set
End Property
End Class

'-------------------------------------------------------------
Public Class Teeth
Private myTooth() As Tooth
Public Sub New()
End Sub
End Class

'-------------------------------------------------------------
Public Class Tooth
Public Sub New()
End Sub
Private pLngth As Integer
Public Property Lngth() As Integer
Get
Return pLngth
End Get
Set(ByVal value As Integer)
pLngth = value
End Set
End Property

===================
so right here ...
inside this instance of Tooth class ...
... how can I discover if the parent Mammal is a Carnivore?
===================

End Class
'-------------------------------------------------------------
 
Ad

Advertisements

J

Jack Jackson

How to access a property of the PARENT custom class instance from a CHILD
custom class instance.

Simply as a convenience for discussion ... let's assume

A Mammal has teeth
Teeth are an array of Tooth's
From inside one instance of the TOOTH class ...
... how do I get access to a property (Carnivore)
in the MAMMAL class?

'-------------------------------------------------------------
Public Class Mammal
Private myTeeth As New Teeth()
Public Sub New()
End Sub
Private pCarnivore As Boolean = False
Public Property Carnivore() As Boolean
Get
Return pCarnivore
End Get
Set(ByVal value As Boolean)
pCarnivore = value
End Set
End Property
End Class

'-------------------------------------------------------------
Public Class Teeth
Private myTooth() As Tooth
Public Sub New()
End Sub
End Class

'-------------------------------------------------------------
Public Class Tooth
Public Sub New()
End Sub
Private pLngth As Integer
Public Property Lngth() As Integer
Get
Return pLngth
End Get
Set(ByVal value As Integer)
pLngth = value
End Set
End Property

===================
so right here ...
inside this instance of Tooth class ...
... how can I discover if the parent Mammal is a Carnivore?
===================

End Class
'-------------------------------------------------------------

..NET has no built-in concept of Parent and Child. Some classes, such
as the WinForms controls, implement such a concept. You will have to
implement it yourself.

I think the first thing I would ask, is why does Tooth need to know
what it 'belongs' to? I don't know the details of your situation, but
often it is a mistake to have a low-level object know anything about
what higher level object it 'belongs' to.

Also, I would use List(Of Teeth) rather than arrays.
 
C

Cor Ligthert[MVP]

FlashMerlot,

Tooth is no child of Mamal. It is a property of a Mamal, nothing more.

Your Tooth is not a Tooth from you. You have toots a child is something
else.

Cor
 
Ad

Advertisements

P

Phill W.

FlashMerlot said:
How to access a property of the PARENT custom class instance from a CHILD
custom class instance.

Each Tooth is a member of a List (of teeth) that belongs to a specific
/instance/ of a class of Mammal. To make that association, you have to
give the teeth an idea of who they belong to and each Tooth an idea of
which "set" of teeth it belongs to, something like:

Public Class Mammal

Public Sub New()
m_Teeth = New Teeth( Me )
End Sub

Public Property Carnivore() As Boolean
Get
Return pCarnivore
End Get
Set(ByVal value As Boolean)
pCarnivore = value
End Set
End Property

Private m_Teeth As Teeth ' no New
Private pCarnivore As Boolean = False

End Class


Public Class Teeth
Inherits List(Of Tooth)

Public Sub New(ByVal owner as Mammal)
m_owner = owner
End Sub

' not ideal, but will do for now
Private m_owner as Mammal

Private ReadOnly Property Owner() as Mammal
Get
Return m_owner
End Get
End Property

End Class

Public Class Tooth

Public Sub New(ByVal ownerSet as Teeth)
m_ownerSet = ownerSet
End Sub

Public ReadOnly Property [Length]() As Integer
Get
Return m_iLength
End Get
End Property

Private m_ownerSet as Teeth = Nothing
Private m_iLength As Integer = 0

Private ReadOnly Property OwnerSet() as Teeth
Get
Return m_ownerSet
End Get
End Property

Private Sub Whatever()
' So now: Does this tooth belong to a carnivore?
If Me.OwnerSet.Owner.Carnivore Then
. . .
End If
End Sub

End Class

HTH,
Phill W.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top