Parent and child

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Parent and child

Post by agibsonsw »

Hello.
I'm still struggling to understand the creation of child/ parent relations between classes. In my child class I have:
Public Property Get Parent() As clsParent
Set Parent = mobjParent
End Property
Public Property Set Parent(aParent As clsParent)
Set mobjParent = aParent
End Property
In my parent class I have:
Private Sub Class_Initialize()
Set mobjChild = New clsChild
Set mobjChild.Parent = Me
End Sub
In a standard module I have:
Dim aChild As clsChild ' the child class
Dim aParent As clsParent ' the parent class
Set aChild = New clsChild
Set aParent = New clsParent
aParent.Name = "Dave"
Set aChild.Parent = aParent
MsgBox "This child's parent's name is " & aChild.Parent.Name
Now although this works, I think I'm missing something in my understanding. Shouldn't I be able to assign parent properties without having to instantiate a (new) parent class?
I would appreciate if someone could point me in the right direction, Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

User avatar
StuartR
Administrator
Posts: 12577
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: Parent and child

Post by StuartR »

agibsonsw wrote:.... Shouldn't I be able to assign parent properties without having to instantiate a (new) parent class?
If you have a variable that points to an object of the correct type you can set this to be the parent. You can either create a new parent object, as in your code, or locate one in some other way. For example you could create a sibling to an existing child object with code like
Set aChild.Parent = bChild.Parent
StuartR


User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: Parent and child

Post by agibsonsw »

Thank you. I read that the Instancing property of my parent class had to be set to PublicNotCreatable for my code to work (as it assigns 'Me') but my code works without this.
Could you shed some light on this for me? Ta, Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

User avatar
HansV
Administrator
Posts: 78235
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Parent and child

Post by HansV »

You need to set the Instancing property if you want to be able to use a class defined in one project (for example an Exce workbook) in another project (such as a Word document or another Excel workbook). If you use the class within the project it is defined in, you don't need to set the Instancing property.
Best wishes,
Hans

User avatar
StuartR
Administrator
Posts: 12577
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: Parent and child

Post by StuartR »

agibsonsw wrote:...Could you shed some light on this for me? Ta, Andy.
I have not come across this before, but KB555159 makes it clear.

Your class can be either Private, or PublicNotCreatable
  • If it is private then it can only be used from within the project where it is defined
  • If it is PublicNotCreatable then it can be used by an external project, but the project that defines the class must provide a public function that creates an instance and returns a pointer to it.
StuartR


User avatar
rory
5StarLounger
Posts: 817
Joined: 24 Jan 2010, 15:56

Re: Parent and child

Post by rory »

Your Parent class automatically creates a Child object for itself when initialised - is that what you meant for it to do? (given that you then create a Child object yourself and assign it to the Parent?
Regards,
Rory

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: Parent and child

Post by agibsonsw »

Hello. That is what confuses me..
I 'borrowed' the parent/class definitions from the internet, but the examples I found didn't show the standard module that uses these.
Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: Parent and child

Post by agibsonsw »

I have not come across this before, but KB555159 makes it clear.

Your class can be either Private, or PublicNotCreatable
  • If it is private then it can only be used from within the project where it is defined
  • If it is PublicNotCreatable then it can be used by an external project, but the project that defines the class must provide a public function that creates an instance and returns a pointer to it.
[/quote]

That is how I understood it, but 'Access 2007 VBA' by Wrox, told me that I wouldn't be able to compile the parent class without changing the Instancing property. Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: Parent and child

Post by agibsonsw »

I have not come across this before, but KB555159 makes it clear.

.. also, whenever I search the Knowledge Base I only tend to find lists of errors and their resolution. Do you know how I can refine my search to find tutorials?
Thanks, Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

User avatar
StuartR
Administrator
Posts: 12577
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: Parent and child

Post by StuartR »

agibsonsw wrote:...Do you know how I can refine my search to find tutorials?
On this particular occassion I simply did a Google search for PublicNotCreatable and the first article in the search result was KB555159.
StuartR


User avatar
rory
5StarLounger
Posts: 817
Joined: 24 Jan 2010, 15:56

Re: Parent and child

Post by rory »

Your posted code doesn't show anything in the Parent class that exposes the Child, but assuming you had a Child property:

Code: Select all

Dim aChild As clsChild ' the child class
Dim aParent As clsParent ' the parent class
Set aParent = New clsParent
aParent.Name = "Dave"
Set aChild = aParent.Child
MsgBox "This child's parent's name is " & aChild.Parent.Name
Regards,
Rory

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: Parent and child

Post by agibsonsw »

Hi. That is, character for character, what I have. It works.
I think perhaps I'm trying to think too deeply into this, and confusing myself.
The whole purpose is to be able to access parent properties using 'aChild.Parent.SomeProperty'..
But when the parent object is initialized, a child object is also 'created' and its parent assigned, even though the child does not (yet) "exist" in a standard module?!
Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

User avatar
HansV
Administrator
Posts: 78235
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Parent and child

Post by HansV »

The code

Code: Select all

Private Sub Class_Initialize()
  Set mobjChild = New clsChild
  Set mobjChild.Parent = Me
End Sub
in the Parent class automatically creates a child each time a parent is created. Now you could view this philosophically (you can't be a parent if you don't have a child) but - as Rory suggested higher up - this code may be unnecessary. I'd try removing it.
Best wishes,
Hans

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: Parent and child

Post by agibsonsw »

Hi. Yes, it works fine without this. I think this is the source of my confusion. I got this from 'Access 2007 VBA' by Wrox - but I found similar code on the "inter-web".
Property Set Parent() is all that is required to create a parent/child relation between two classes?
Isn't the parent/child metaphor misleading, because OOP requires that a child have only one parent? Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

User avatar
HansV
Administrator
Posts: 78235
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Parent and child

Post by HansV »

agibsonsw wrote:Property Set Parent() is all that is required to create a parent/child relation between two classes?
Yes, that is sufficient.
agibsonsw wrote:Isn't the parent/child metaphor misleading, because OOP requires that a child have only one parent?
It is misleading in a sense, you shouldn't interpret the metaphor too literally. But if you wanted to model family relationships, you could give a "child" two parent properties, named "father" and "mother".
Best wishes,
Hans

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: Parent and child

Post by agibsonsw »

I won't worry too much about uncles and aunts.. Thanks, Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.