Accessing a Parent Object's Properties

R

RSH

Hi,

I have a situation where I have a Parent Object (Company) which has several
public properties. The Company Object also creates an Employees object
which has its ow set of functions and properties. I am trying to figure out
how to access the Parent properties from the child object.

Simplified example:
Class Company
Public ReadOnly Property Employees() As Employees



Public Sub New(ByVal CompanyID As String)

_CompanyID = CompanyID

_Employees = New Employees(_CompanyID)

End Sub



Get

Return _Employees

End Get

End Property

Public ReadOnly Property CompanyID() As String

Get

Return _CompanyID

End Get

End Property

Public Property DateLow() As Date

Get

Return _DateLow

End Get

Set(ByVal Value As Date)

_DateLow = Value

End Set

End Property

Public Property DateHi() As Date

Get

Return _DateHi

End Get

Set(ByVal Value As Date)

_DateHi = Value

End Set

End Property

End Class





Class Employees

End Class



Thanks,

Ron
 
R

RobinS

Add a reference to the Company in the Employees class. When you create the
Employees class, fill in reference with the parent that created it.

Robin S.
------------------------------
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

You don't. If the child doesn't have the information, it doesn't. You
have to put the information there from the parents perspective.

You can add a reference to the company as a parameter to the constructor
of the Employees collection.
 
P

Phill W.

RSH said:
I have a situation where I have a Parent Object (Company) which has several
public properties. The Company Object also creates an Employees object
which has its ow set of functions and properties. I am trying to figure out
how to access the Parent properties from the child object.

Something like this:

Class Parent
Public Sub New(name As String)
m_sName = name
End Sub

Public Function GetEmployee(empNo as String) as Employee
Return New Employee(Me, empNo)
End Function

Public ReadOnly Property Name() as String
Get
Return m_sName
End Get
End Property

Private m_sName as String = "Bogus Co."

End Class

Class Employee
' Prevent access to default constructor
Private Sub New()
End Sub

Friend Sub New(parent as Company, empNo as String)
m_oParent = parent
m_empNo = empNo
End Sub

Public ReadOnly Property Company() as Company
Get
Return m_oParent
Get
End Property

Public ReadOnly Property Name() as String
Get
Return m_sName
End Get
End Sub

Private m_oParent as Company
Private m_sName as String

End Class

.... then ...

Dim c as New Company( "Bogus Co." )
Dim e as c.GetEmployee( "ABC123" )

? c.Company.Name
"Bogus Co."

HTH,
Phill W.
 
R

RobinS

Phill W. said:
Something like this:

Class Parent
Public Sub New(name As String)
m_sName = name
End Sub

Public Function GetEmployee(empNo as String) as Employee
Return New Employee(Me, empNo)
End Function

Public ReadOnly Property Name() as String
Get
Return m_sName
End Get
End Property

Private m_sName as String = "Bogus Co."

End Class

Class Employee
' Prevent access to default constructor
Private Sub New()
End Sub

Friend Sub New(parent as Company, empNo as String)
m_oParent = parent
m_empNo = empNo
End Sub

Public ReadOnly Property Company() as Company
Get
Return m_oParent
Get
End Property

Public ReadOnly Property Name() as String
Get
Return m_sName
End Get
End Sub

Private m_oParent as Company
Private m_sName as String

End Class

... then ...

Dim c as New Company( "Bogus Co." )
Dim e as c.GetEmployee( "ABC123" )

? c.Company.Name
"Bogus Co."

HTH,
Phill W.

Nice job.

Robin S.
 
P

Phill W.

RobinS said:
Nice job.

Robin S.

Lousy employer, though ...

Sure, their employees have Names, not numbers, but what about Salary!!
;-)

Many Thanks,
Phill W.
 
R

RobinS

Phill W. said:
Lousy employer, though ...

Sure, their employees have Names, not numbers, but what about Salary!!
;-)

Many Thanks,
Phill W.

LOL.

Robin S.
 

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