473,480 Members | 1,669 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Object Hierarchy Problem

Hi,

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

ProductNode
---Product Node
---ProductNode
------ProductNode
------ProductNode
---ProductNode
---ProductNode
------ProductNode
etc

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
etc

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
Get
Return _product.Depth
End Get
End Property

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

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

'Expose the product Name property
Public ReadOnly Property Name() As String
Get
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) + "," +
Convert.ToString(product.Depth)
_product = product
End Sub

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

'Removes a product from the children of this product node
Public Sub Remove(ByVal productNode As ProductNode)
_children.Remove(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
_productData.Read()

' 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
product
Dim thisProductNode As New ProductNode(thisProduct)

'Add this node to the parent
parentNode.Add(thisProductNode)

' Recurse this function using the current product node
BuildHierarchy(thisProductNode)

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
possible.

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!

A
Nov 22 '05 #1
0 945

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1383
by: archway | last post by:
Hi, I would like to build a hierarchy of ProductNode objects like so ProductNode ---Product Node ---ProductNode ------ProductNode ------ProductNode ---ProductNode
5
5026
by: wbekker | last post by:
Hi, I'm searching for a good pattern for the following problem: In a large object tree, all object implement a property called IsDirty. That flag is set when a property is modified. If a child...
16
10063
by: Elad | last post by:
Hi, I have an application that is made up of several executables. I need all these executables to use the same instance of an object. What is the best, most efficient way to approach this? ...
13
2159
by: ahaupt | last post by:
Hi all, I'm implementing the Clone() method through the ICloneable interface and don't quite know how deep I need to go for a deep copy. Example: class A: ICloneable { object _val;
15
26196
by: mr.peteryu | last post by:
Hi, Can someone explain the idea behind casting to an interface? For example: -> I have an IInterface that contains a Read() method. -> I have an object "obj" that implements IInterface. ...
16
2869
by: anonymous.user0 | last post by:
The way I understand it, if I have an object Listener that has registered as a listener for some event Event that's produced by an object Emitter, as long as Emitter is still allocated Listener...
3
1790
by: Techno_Dex | last post by:
I'm wanting to create a Wrapper (or Extender depending on how you look at it) for a Serializable object that I then want to send over a webservice. Basically I want to create a Serializable Object,...
17
2190
by: Jef Driesen | last post by:
Suppose I have a datastructure (actually it's a graph) with one template parameter (the property P for each edge and vertex): struct graph<P>; struct vertex<P>; struct edge<P>; I also have...
11
1340
by: John A Grandy | last post by:
I'm in a vigorous debate at my work regarding objects assuming knowledge of the type their containing object. This debate pertains specifically to ASP.NET, but I have decided to post in the C#...
7
1680
by: joproulx | last post by:
Hi, I was wondering if there was a way with Reflection to find dynamically if an object was referencing indirectly another object. A simple example would be: Object1 | --Object2 |
0
7037
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6904
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7034
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7076
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
6886
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4768
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
2976
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1294
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
174
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.