-
Apr 11th, 2009, 09:16 PM
#1
Thread Starter
Addicted Member
[RESOLVED]reference parent class from inside child class
I have a 'property management class' that contains several functions and properties. I have several other classes that use the 'management class' as a property to derive the value of several of its other properties. Is there a way for me to reference the outer class from the 'management class' functions without having to pass it as a parameter? I ask because several different classes can have this 'management class' as a property and am having a hard time typing the parameter.
Last edited by DemonMade; Apr 13th, 2009 at 08:57 AM.
Reason: [RESOLVED]
No one is perfect, but it doesn’t hurt to try.
-
Apr 11th, 2009, 09:23 PM
#2
Re: reference parent class from inside child class
In the management class add a constructor that accepts the class name.
vb.net Code:
Public Sub New(byval parent as Object) me.Parent = parent End Sub
So when you create this management class object, you can do it in this way:
vb.net Code:
Dim myPM as New ManagementClass(Me)
Inside the management class you can now use the Parent variable/property to reference the parent class.
-
Apr 11th, 2009, 09:25 PM
#3
Thread Starter
Addicted Member
Re: reference parent class from inside child class
I run into the problem of needing to cast type Object to the type of it's parent to reference it's parent's properties, any ideas?
Last edited by DemonMade; Apr 11th, 2009 at 09:29 PM.
No one is perfect, but it doesn’t hurt to try.
-
Apr 12th, 2009, 04:20 AM
#4
Re: reference parent class from inside child class
If you know exactly how your paret/child relationship is going to be (which is most likely the case,) then just bind the class type of the parent:
Code:
_Parent as TheParentsType
Public Sub New(byval parent as TheParentsType)
_Parent = parent
End Sub
-
Apr 12th, 2009, 06:03 AM
#5
Re: reference parent class from inside child class
It is most likely that the parent type would be different in each case. Otherwise the need would not have arosen.
So what you would need to do is either parse it (if there are very few types), or pass the type too along with it.
The best way though would have been to make those classes inherit from the same Base Class so that you could just typecast it to the base class type.
-
Apr 12th, 2009, 09:25 AM
#6
Thread Starter
Addicted Member
Re: reference parent class from inside child class
I will either inherit from a common base or put these properties into a separate class property and pass that by ref.
No one is perfect, but it doesn’t hurt to try.
-
Apr 12th, 2009, 07:57 PM
#7
Thread Starter
Addicted Member
Re: reference parent class from inside child class
If I pass the parent class as a parameter to the management class' constructor ByRef, can I save that to a class level variable and still have it work ByRef so I can just set properties without having to somehow having a way to return them? The reason I ask is I don't want to have to pas the parent to the functions each time they are called.
parentClass.Management.Calculate()
rather than
parentClass.Management.Calculate(parentClass)
No one is perfect, but it doesn’t hurt to try.
-
Apr 13th, 2009, 01:53 AM
#8
Re: reference parent class from inside child class
Originally Posted by DemonMade
If I pass the parent class as a parameter to the management class' constructor ByRef, can I save that to a class level variable and still have it work ByRef so I can just set properties without having to somehow having a way to return them? The reason I ask is I don't want to have to pas the parent to the functions each time they are called.
parentClass.Management.Calculate()
rather than
parentClass.Management.Calculate(parentClass)
Classes are always BYRef in effect, whether you pass it ByRef or ByVal.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|