473,418 Members | 2,037 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,418 software developers and data experts.

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
Jul 21 '05 #1
0 1381

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

Similar topics

5
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
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
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;
0
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
15
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
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
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
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...
7
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.