Results 1 to 8 of 8

Thread: [RESOLVED]reference parent class from inside child class

  1. #1

    Thread Starter
    Addicted Member DemonMade's Avatar
    Join Date
    Dec 2003
    Location
    Holt. MI
    Posts
    179

    Resolved [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.

  2. #2
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: reference parent class from inside child class

    In the management class add a constructor that accepts the class name.

    vb.net Code:
    1. Public Sub New(byval parent as Object)
    2.     me.Parent = parent
    3. End Sub
    So when you create this management class object, you can do it in this way:
    vb.net Code:
    1. Dim myPM as New ManagementClass(Me)

    Inside the management class you can now use the Parent variable/property to reference the parent class.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  3. #3

    Thread Starter
    Addicted Member DemonMade's Avatar
    Join Date
    Dec 2003
    Location
    Holt. MI
    Posts
    179

    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.

  4. #4
    Frenzied Member Campion's Avatar
    Join Date
    Jul 2007
    Location
    UT
    Posts
    1,098

    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

  5. #5
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    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.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  6. #6

    Thread Starter
    Addicted Member DemonMade's Avatar
    Join Date
    Dec 2003
    Location
    Holt. MI
    Posts
    179

    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.

  7. #7

    Thread Starter
    Addicted Member DemonMade's Avatar
    Join Date
    Dec 2003
    Location
    Holt. MI
    Posts
    179

    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.

  8. #8
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: reference parent class from inside child class

    Quote Originally Posted by DemonMade View Post
    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.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width