473,545 Members | 2,444 Online
Bytes | Software Development & Data Engineering Community
+ 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.Parent ID
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.ToStrin g(product.ID) + "," +
Convert.ToStrin g(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(p roductNode)
End Sub

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

' Get all the product fields
Dim name As String = Convert.ToStrin g(_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.Dept h + 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(thi sProduct)

'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 957

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

Similar topics

0
1392
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
5041
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 object is dirty, the parent object must return IsDirty = true as well. This works. I now want to have an event on the root object of the tree,...
16
10083
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? Thanks a lot!
13
2173
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
26229
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. Why would someone do the following and what does it mean?
16
2880
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 will stay alive. Is this correct? If this is correct, I've got a problem. Let's say I've got an object Customer that has an PurchaseList...
3
1804
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, which has some properties on it, but I want to make one of the Properties a generic type that I can assign one of multiple Serializable object. ...
17
2199
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 an algorithm that modifies this datastructure. The basic outline of the algorithm is independent of the type of property. So I implemented a generic...
11
1342
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# forum because this is where most of the OO gurus hang out, and I view this as a fundamental issue of OO design. In ASP.NET, objects of type WebForm...
7
1687
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
7490
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7935
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7780
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6009
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5351
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3465
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1911
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 we have to send another system
1
1037
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
734
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.