Custom Classes - HowTo access property of parent instance from child instance
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Custom Classes - HowTo access property of parent instance from child instance

Hybrid View

  1. #1
    Join Date
    Dec 2007
    Posts
    1

    Question Custom Classes - HowTo access property of parent instance from child instance

    How to access a property of the PARENT custom class instance from a CHILD custom class instance.

    Simply as a convenience for discussion ... let's assume

    A Mammal has teeth
    Teeth are an array of Tooth's
    From inside one instance of the TOOTH class ...
    ... how do I get access to a property (Carnivore) in the MAMMAL class?

    '-------------------------------------------------------------
    Public Class Mammal
    Private myTeeth As New Teeth()
    Public Sub New()
    End Sub
    Private pCarnivore As Boolean = False
    Public Property Carnivore() As Boolean
    Get
    Return pCarnivore
    End Get
    Set(ByVal value As Boolean)
    pCarnivore = value
    End Set
    End Property
    End Class

    '-------------------------------------------------------------
    Public Class Teeth
    Private myTooth() As Tooth
    Public Sub New()
    End Sub
    End Class

    '-------------------------------------------------------------
    Public Class Tooth
    Public Sub New()
    End Sub
    Private pLngth As Integer
    Public Property Lngth() As Integer
    Get
    Return pLngth
    End Get
    Set(ByVal value As Integer)
    pLngth = value
    End Set
    End Property

    ===================
    so right here ...
    inside this instance of Tooth class ...
    ... how can I discover if the parent Mammal is a Carnivore?
    ===================

    End Class
    '-------------------------------------------------------------
    Last edited by volking; February 23rd, 2009 at 11:22 AM. Reason: Text lost indentation - tried to put it back - arrrrrgh!

  2. #2
    Join Date
    Jan 2009
    Location
    Cochin, India
    Posts
    40

    Smile Re: Custom Classes - HowTo access property of parent instance from child instance

    Hi,

    What you are asking for is possible if child classes inherit from parent classes. The way you have written the classes the child classes are elements of parent classes. They are not inheriting from the parent. Then at any given point you cannot find out if a particular Tooth object is a member of an array of tooth inside a teeth object as there is no reverse connection between Tooth and Teeth except that Teeth contains an array of Tooth and any particular Tooth object may or may not be a member of an array inside a Teeth object. So to incorporate your analogy of mammals having Teeth and Teeth having an array of Tooth the child classes will have to inherit from the parent classes. That is the Teeth class has to inherit from Mammal and the Tooth class has to inherit from Teeth. To incorporate the fact that Teeth has to contain an array of Tooth objects and every Mammal has to have a Teeth object I have rewritten the classes as given below.

    Class Mammal

    Code:
    Public Class Mammal     
        Private t As Teeth
        Public Sub New()
        End Sub
        Private pCarnivore As Boolean = False
        Public Property Carnivore() As Boolean
            Get
                Return pCarnivore
            End Get
            Set(ByVal value As Boolean)
                pCarnivore = value
            End Set
        End Property
        Public Property GetAndSetTeeth() As Teeth
            Get
                Return t
            End Get
            Set(ByVal value As Teeth)
                t = value
            End Set
        End Property
    End Class
    Class Teeth

    Code:
    Public Class Teeth
        Inherits Mammal
        Private myTooth() As Tooth
        Public Sub New()
        End Sub
        Public ReadOnly Property GetTooth() As Tooth()
            Get
                Return myTooth
            End Get
        End Property
        Public Property SetTooth() As Integer
            Get
                Return myTooth.Length
            End Get
            Set(ByVal value As Integer)            
                myTooth = New Tooth(value) {}
                Dim i As Integer = 0
                While i < myTooth.Length
                    myTooth(i) = New Tooth
                    i = i + 1
                End While
    
            End Set
        End Property
    End Class

    Class Tooth

    Code:
    Public Class Tooth
        Inherits Teeth
        Public Sub New()
        End Sub
        Private pLngth As Integer
    
        Public Property Lngth() As Integer
            Get
                Return pLngth
            End Get
            Set(ByVal value As Integer)
                pLngth = value
            End Set
        End Property
    End Class
    Now create the classes and open a windows form and put a button and you can put the following code in the button click event and run the form.

    Code:
            Dim t As Tooth = New Tooth
            t.Carnivore = True
            MessageBox.Show(t.Carnivore)
            t.Carnivore = False
            MessageBox.Show(t.Carnivore)
    
            Dim m As Mammal = New Mammal
            Dim t1 As Teeth = New Teeth
            m.GetAndSetTeeth = t1
            t1.SetTooth = 5
            Dim tp() As Tooth = m.GetAndSetTeeth().GetTooth
            Dim i As Integer = 0
            While i < tp.Length
                If (i Mod 2 = 0) Then
                    tp(i).Carnivore = False
                Else
                    tp(i).Carnivore = True
                End If
                i = i + 1
            End While
            i = 0
            While i < tp.Length
                MessageBox.Show(tp(i).Carnivore)
                i = i + 1
            End While
    Hope that helps demonstrate how classes and inheritance works.

    Warm Regards.
    Quote Originally Posted by volking View Post
    How to access a property of the PARENT custom class instance from a CHILD custom class instance.

    Simply as a convenience for discussion ... let's assume

    A Mammal has teeth
    Teeth are an array of Tooth's
    From inside one instance of the TOOTH class ...
    ... how do I get access to a property (Carnivore) in the MAMMAL class?

    '-------------------------------------------------------------
    Public Class Mammal
    Private myTeeth As New Teeth()
    Public Sub New()
    End Sub
    Private pCarnivore As Boolean = False
    Public Property Carnivore() As Boolean
    Get
    Return pCarnivore
    End Get
    Set(ByVal value As Boolean)
    pCarnivore = value
    End Set
    End Property
    End Class

    '-------------------------------------------------------------
    Public Class Teeth
    Private myTooth() As Tooth
    Public Sub New()
    End Sub
    End Class

    '-------------------------------------------------------------
    Public Class Tooth
    Public Sub New()
    End Sub
    Private pLngth As Integer
    Public Property Lngth() As Integer
    Get
    Return pLngth
    End Get
    Set(ByVal value As Integer)
    pLngth = value
    End Set
    End Property

    ===================
    so right here ...
    inside this instance of Tooth class ...
    ... how can I discover if the parent Mammal is a Carnivore?
    ===================

    End Class
    '-------------------------------------------------------------
    Last edited by sr_jay; February 25th, 2009 at 04:04 AM.
    Jay
    Support Resort
    http://www.supportresort.com
    Bringing offshore expertise to the world

Tags for this Thread

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center