Parent and child
-
- SilverLounger
- Posts: 2403
- Joined: 05 Feb 2010, 22:21
- Location: London ENGLAND
Parent and child
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 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.
-
- Administrator
- Posts: 12322
- Joined: 16 Jan 2010, 15:49
- Location: London, Europe
Re: Parent and child
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 likeagibsonsw wrote:.... Shouldn't I be able to assign parent properties without having to instantiate a (new) parent class?
Set aChild.Parent = bChild.Parent
StuartR
-
- SilverLounger
- Posts: 2403
- Joined: 05 Feb 2010, 22:21
- Location: London ENGLAND
Re: Parent and child
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.
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.
-
- Administrator
- Posts: 76688
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Parent and child
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.
Regards,
Hans
Hans
-
- Administrator
- Posts: 12322
- Joined: 16 Jan 2010, 15:49
- Location: London, Europe
Re: Parent and child
I have not come across this before, but KB555159 makes it clear.agibsonsw wrote:...Could you shed some light on this for me? Ta, Andy.
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
-
- 5StarLounger
- Posts: 784
- Joined: 24 Jan 2010, 15:56
Re: Parent and child
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
Rory
-
- SilverLounger
- Posts: 2403
- Joined: 05 Feb 2010, 22:21
- Location: London ENGLAND
Re: Parent and child
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 '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.
-
- SilverLounger
- Posts: 2403
- Joined: 05 Feb 2010, 22:21
- Location: London ENGLAND
Re: Parent and child
I have not come across this before, but KB555159 makes it clear.
Your class can be either Private, or PublicNotCreatable
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.
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.
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.
-
- SilverLounger
- Posts: 2403
- Joined: 05 Feb 2010, 22:21
- Location: London ENGLAND
Re: Parent and child
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.
.. 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.
-
- Administrator
- Posts: 12322
- Joined: 16 Jan 2010, 15:49
- Location: London, Europe
Re: Parent and child
On this particular occassion I simply did a Google search for PublicNotCreatable and the first article in the search result was KB555159.agibsonsw wrote:...Do you know how I can refine my search to find tutorials?
StuartR
-
- 5StarLounger
- Posts: 784
- Joined: 24 Jan 2010, 15:56
Re: Parent and child
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
Rory
-
- SilverLounger
- Posts: 2403
- Joined: 05 Feb 2010, 22:21
- Location: London ENGLAND
Re: Parent and child
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 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.
-
- Administrator
- Posts: 76688
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Parent and child
The code
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.
Code: Select all
Private Sub Class_Initialize()
Set mobjChild = New clsChild
Set mobjChild.Parent = Me
End Sub
Regards,
Hans
Hans
-
- SilverLounger
- Posts: 2403
- Joined: 05 Feb 2010, 22:21
- Location: London ENGLAND
Re: Parent and child
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.
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.
-
- Administrator
- Posts: 76688
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Parent and child
Yes, that is sufficient.agibsonsw wrote:Property Set Parent() is all that is required to create a parent/child relation between two classes?
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".agibsonsw wrote:Isn't the parent/child metaphor misleading, because OOP requires that a child have only one parent?
Regards,
Hans
Hans
-
- SilverLounger
- Posts: 2403
- Joined: 05 Feb 2010, 22:21
- Location: London ENGLAND
Re: Parent and child
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.