Nested classes: how to access parent class data?

Siebe

New member
Joined
Dec 23, 2006
Messages
2
Programming Experience
Beginner
Hi all!

I've got a little question regarding nested classes.
Is there a method in VB.NET that allows you to access data from a parent class in case you're working in a subclass?

I'm thinking of something like:
Me.Parent.Parent.Parent.Data

The reason is that I'd like to access data in the root class without having to pass on that data though each nestinglayer.
I use the following classes in a nested situation:

Class General
myNodeTypes as new enumNodeType
myCases as new list of Case
End Class

Class Case
myNetwork as new network
end Class

Class Network
myNodes as new list of Node
end Class

Class Node
Public ID as string
Public myNodeType as enmNodeType
end Class

As you can see, I'll have to access data (the enumerator data) that is stored in the root class "General" while creating a new instance of "Node". Well obviously I could pass on that enumerator data through every nesting step, but I don't think that's very elegant. Is there another way?

Any ideas will be appreciated! Thanks
Siebe
 
Last edited:
The requested relationship between general.nodetype and node.nodetype is not clear. Do you want all nodes to have same nodetype as general?

Here is a basic example doing this:
VB.NET:
Enum NodeType
    type1
    type2
End Enum
 
Class General
    Inherits System.Collections.ObjectModel.Collection(Of Node)
    Public myNodeType As NodeType
    Protected Overrides Sub InsertItem(ByVal index As Integer, ByVal item As Node)
        item.parent = Me
        MyBase.InsertItem(index, item)
    End Sub
End Class
 
Class Node
    Public parent As General
    Public ID As String
    Sub New(ByVal id As String)
        Me.ID = id
    End Sub
End Class
usage:
VB.NET:
Sub usegeneralnodes()
    Dim generalinstance As New General
    generalinstance.myNodeType = NodeType.type1
    generalinstance.Add(New Node("n123"))
 
    MsgBox(generalinstance.Item(0).parent.myNodeType.ToString)
End Sub
 
Hei John, hvordan har du det?

I'm not sure whether I fully understand your solution yet, but I'll study it a bit harder. In the mean time: let me answer your question.

First of all: let me tell you I've replaced the enumerator by a class.

General.Node(iNode).myNodetype is basically being filled using a lookup function that loops through General.Nodetypes. I do this because in the "Node" class I already know the node type (string), but I want to lookup the corresponding node type number as well.

VB.NET:
'each node type has a name and a corresponding number
Public Class NodeType
  Dim strNodeType as string
  Dim NodeTypeNumber as integer

  Public Sub New(iNodeType as string, iNodeTypeNumber as integer)
    'so here I'll assign the name and number of each node type
    strNodeType = iNodeType
    NodeTypeNumber = iNodeTypeNumber
  End Sub
End Class

Public Class General
  'as you can see the nodetypes are defined on this level
  Dim NodeTypes as new List(of NodeType)
  Dim Nodes as new List(of Node)
End Class

Public Class Node
'in this class I want to be able to access NodeTypes, which are defined on a higher level (General)
  Dim myNodetype as NodeType

  Public sub New(iNodeType as string)
  'loop through the nodetypes as defined in the class "general"
  For i = 0 to ubound(me.parent.Nodetype)
    If iNodeType = me.parent.nodetype(i).strNodeType then 
      myNodeType = iNodeType 
      Exit For
    End If
  Next i
  End Sub
End Class
 
There is no relationship between a class instance and the collection instance it is contained in, there exist no programming/language element that makes this happen by default. My example above solves this by adding the 'parent' member to node class, and also setting this value when the node is added to the general collection. You can use the same method to set the nodes type reference when node is added to collection. Alternative is to add a factory method to the general class that will create node instances and add to its collection.

God Jul! Merry Christmas!
 
Back
Top