473,769 Members | 6,499 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
Jul 21 '05 #1
0 1410

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

Similar topics

5
5056
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, that signals when one object in the tree is set to dirty. What is a good approach for
16
10137
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
2197
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
966
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
26300
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
2900
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 (Collection) of Purchase objects. Now, these Purchase objects were pulled from a datasource Datasource. The...
3
1820
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. I've look at creating it using an XmlNode and an object but so far both appear to have issues. I've...
17
2237
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 version of the algorithm and a function object for
7
1695
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
9587
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9993
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9863
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8870
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7406
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5298
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3958
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

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.