469,908 Members | 2,123 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,908 developers. It's quick & easy.

Object Hierarchy Problem


I would like to build a hierarchy of ProductNode objects like so

---Product Node

The data that populates these objects comes from a stored procedure that
returns it in the following format:

Depth Id ParentId
0 1 0
1 2 1
1 3 1
2 45 3
2 56 3
3 98 56
2 89 3

The definition of the ProductNode class is as follows (unfortunately in VB):

Public Class ProductNode

Private _key As String = String.Empty
Private _product As Product
Private _children As ArrayList = New ArrayList

' Expose the product depth property
Public ReadOnly Property Depth() As Int16
Return _product.Depth
End Get
End Property

' Expose the product ID property
Public ReadOnly Property ID() As Int32
Return _product.ID
End Get
End Property

' Expose the product ParentID property
Public ReadOnly Property ParentID() As Int32
Return _product.ParentID
End Get
End Property

'Expose the product Name property
Public ReadOnly Property Name() As String
Return _product.Name
End Get
End Property

'Constructor that stores the product and creates a key based upon the
product ID and depth
Public Sub New(ByVal product As Product)
_key = Convert.ToString(product.ID) + "," +
_product = product
End Sub

'Adds a product to the children of this product node
Public Sub Add(ByVal productNode As ProductNode)
End Sub

'Removes a product from the children of this product node
Public Sub Remove(ByVal productNode As ProductNode)
End Sub

End Class

I am then using the following method to populate the entire hierarchy. The
first time this is called from another method the productnode that is passed
as a parameter is the root node:

Private Sub BuildHierarchy(ByVal parentNode As ProductNode)

' Read the next item in the data

' Get all the product fields
Dim name As String = Convert.ToString(_productData("Name"))
Dim id As Int32 = Convert.ToInt32(_productData("Idx"))
Dim depth As Int16 = Convert.ToInt16(_productData("Depth"))
Dim parentID As Int32 = Convert.ToInt32(_productData("ParentIdx"))

' If this item is a child of the passed in parent add it
If parentID = parentNode.ID And depth = parentNode.Depth + 1 Then

'Instantiate a new product with these fields
Dim thisProduct = New Product(name, id, depth, parentID)

'Instantiate a new product node for the tree that contains the
Dim thisProductNode As New ProductNode(thisProduct)

'Add this node to the parent

' Recurse this function using the current product node

End If

End Sub

This works fine for the first branch of the hierarchy and will product an
output similar to:

All Products
---Product branch 1 level 1
------Product branch 1 level 2
---------Product branch 1 level 3
------------Product branch 1 level 4
------Product branch 2 level 2 <-- next item not sure how to get

However, I am unsure of how to then populate the rest of the hierarchy. The
rows returned from the stored procedure are returned in such a form that
branch is populated from left to right. So, for example in the above example
the rows would come out in the following order:

All Products
Product branch 1 level 1
Product branch 1 level 2
Product branch 1 level 3
Product branch 1 level 4
Product branch 2 level 2 <-- next item not sure how to get

So, the BuildHierarchy function above would recurse through until "Product
branch 1 level 4" and then exit. The next row in the returnset from the
stored procedure is "Product branch 2 level 2". I cannot think of how to add
this child to its parent (Product branch 1 level 1) within the object model.
Perhaps I could use the key property of the object to navigate to the parent
directly, but I would have to search through the entire hierarchy to do this
and it would take time considering there could be thousands of objects.

I was also contemplating the possibility of somehow linking the parent from
the child, but was not sure whether this would be good practice, relevant or

Does anyone have any ideas on how I could achieve this?

Many thanks in advance. If you have read the whole thing I am impressed!

Jul 21 '05 #1
0 1259

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by archway | last post: by
15 posts views Thread by mr.peteryu | last post: by
16 posts views Thread by anonymous.user0 | last post: by
3 posts views Thread by Techno_Dex | last post: by
17 posts views Thread by Jef Driesen | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.