Question Organizing classes and properties

keb1965

Well-known member
Joined
Feb 9, 2009
Messages
103
Programming Experience
10+
I need to access a few properties in a parent class from the inherited class of an object.

I hope that makes sense ...

Anyway, here is the scenario:

VB.NET:
'This is the parent class with all the parent information
Class Parent

  Private _children As Children
  Private _Father As String
  Private _Mother As String

End Class

'This is the children class .. a collection of child
Class Children : Inherits List(Of Child)
'Do stuff here
End Class

'This is the inherited child class .. it needs to access the parent
'class information so it can act on the values in the parent
'i.e. the child wants to know "Who's my daddy?"
Class Child

Public ReadOnly Property Father As String
   Get
       Return _Father
   End Get
End Property

Public ReadOnly Property Mother As String
   Get
       Return _Mother
   End Get
End Property

End Class

Maybe its a rookie question, but it doesn't seem obvious to me without writing lots of code to propagate the values to the child classes .. and in my case that will be hundreds.
 
In future, please ask your question in your post, not in comments in the code.

Anyway, if the Child object needs to access the Parent object members then go ahead. Based on what you have there, you would use Me.Father inside your Child object to access the Parent object that represents the father, from there you simply get whatever members you want to use, e.g. Me.Father.Name.
 
The scenario you mention is certainly available for the inherited class(es) but not from the instantiated class. i.e. from the example, Child can access any methods and/or properties in Children, but not Parent.
I suppose if Parent inherited Children it would be able to access the properties, but I am not sure it should inherit children.

I am sure this is unnecessary information, but the project I am currently working on needs a base class that can be inherited by any number of unknown classes. The express purpose is to translate data from unknown formats into objects that have a predetermined structure as defined by the base class, that can be easily manipulated by the program.

It is basically a "plug-in" loader for an existing application.
Our software imports data from various other applications, currently about 10 different file formats, then uses it to generate related data. As more file formats are supported, the need to compartmentalize the import process becomes a necessity. Looking back at code written 7 or 8 years ago, it becomes very obvious, as the code is certainly unwieldy in the data import area.

Anyway, the class is currently organized something like the previous example ... but here it is again just for posterity, only in greater detail .. note that the derived class actually does the translation and merely categorizes the data into something the program can understand.

VB.NET:
Public MustInherit Class DataReader

     Private m_InputFile As String = ""
     Private m_FormatName As String = ""
     Private m_DataCollection As New DataCollection
     Private m_Prefix As String = ""
     Private m_Suffix As String = ""
     Private m_Separator1 As String = ""
     Private m_Separator2 As String = ""

'Public Properties for Class DataReader

'Derived classes actually do the heavy lifting
Public MustOverride ReadData() As Boolean

End Class

Public Class DataCollection : Inherits List(Of Data)

'Overloads and Shadows removed for brevity

End Class

Public Class Data

     Private m_ID As String = ""
     Private m_Volume As New Volumes
     Private m_Name As New Names
     Private m_Type As TypeEnum

'Public Properties and Enum

End Class

Public Class Volumes : Inherits List(Of Volume)

'Overloads and Shadows removed for brevity

End Class

Public Class Volume

     Private m_CountA As Integer = 0
     Private m_CountB As Integer = 0
     Private m_CountC As Integer = 0
     Private m_CountType As CountTypeEnum

'Public Properties and Enum

End Class

Public Class Names : Inherits List(Of Name)

'Overloads and Shadows removed for brevity

End Class

Public Class Name

     Private m_Name As String
     Private m_Direction As DirectionEnum

'Public Properties and Enum

End Class

Given the code segment above, would it be best to simply inherit the DataCollection class? It seems now that I have gotten some sleep that it seems to be the logical solution, then everything would be available within the class structure.

Also, given that each derived class has properties that may be accessed from the unknown class (the class that actually implements the ReadData function), is it best to simply access each class' properties through the exposed properties or by accessing friend variables.

I'd appreciate advice on organization and I value your input, seeing as you helped me a great deal when I was the sole developer on a huge project a few years ago. However, this is the first time I have done something where the class is specifically designed to be inherited.
 
I misread your example, due to your poor naming. I saw the Father and Mother properties in the Child class and assumed that they were instances of the Parent class. I missed that they were declared type String, which indicates that they are actually the names of the parents rather than the parents themselves, which means that they should be named FatherName and MotherName. Anyway, I think I have a better understanding of what you want now and, if I'm right, here's how I would implement it:
Public Class Parent

    Private _children As New List(Of Child)

    Public Property Name As String

    Public ReadOnly Property Children As List(Of Child)
        Get
            Return _children
        End Get
    End Property

End Class

Public Class Child

    Private _mother As Parent
    Private _father As Parent

    Public Property Mother As Parent
        Get
            Return _mother
        End Get
        Set(value As Parent)
            If _mother IsNot value Then
                _mother = value
                _mother.Children.Add(Me)
            End If
        End Set
    End Property

    Public Property Father As Parent
        Get
            Return _father
        End Get
        Set(value As Parent)
            If _father IsNot value Then
                _father = value
                _father.Children.Add(Me)
            End If
        End Set
    End Property

End Class
Now, if you set the Mother or Father property of a Child object, the Child will be automatically added to or removed from that Parent object's Children collection. If you want to be able to do the reverse, i.e. add a Child to a Parent's Children collection and have the Mother or Father set automatically, then you'd have to put that logic in the collection itself, but that's not really practical when a child has two parents and you have no way to know whether the Parent object represents the mother or father.
 
Also notice the special relations here; that neither parent or child is a person, and that a parent can't have a parent, and that a child can't have a child... isn't that odd? All this talk about inheritance, and then there isn't any. Has it just got lost in translation?
 
Back
Top