473,386 Members | 1,823 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,386 software developers and data experts.

Accessing objects in a hierarchy?

I have an object hierarchy that's several layers deep, and I'm puzzling over
how to access an object at any point in the hierarchy, solely by its key.

The hierarchy is similar to an organizational chart, with a CEO at the top,
his/her subordinates in the next layer, their subordinates in the layer
below that, and so on. Each person is represented by an Employee object, and
Each Employee object has a Subordinates collection property (a dictionary),
which contains references to the Employee objects that represent their
subordinates, keyed by EmployeeID. So, the Employee objects are distributed
among a number of Subordinate collections.

Here's my problem: I need to get to any Employee object in the hierarchy,
based solely on its EmployeeID. I won't have a complete key path down the
hierarchy to the object. What's the best way to do this?

The obvious solution to me is to create another dictionary that contains
object references to all Employee objects, which I would use to access
employees by ID number. But is there a better solution? Thanks.

--
Dave Veeneman
Chicago
Nov 15 '05 #1
4 2832
Dave Veeneman <da****@nospam.com> wrote:
I have an object hierarchy that's several layers deep, and I'm puzzling over
how to access an object at any point in the hierarchy, solely by its key.

The hierarchy is similar to an organizational chart, with a CEO at the top,
his/her subordinates in the next layer, their subordinates in the layer
below that, and so on. Each person is represented by an Employee object, and
Each Employee object has a Subordinates collection property (a dictionary),
which contains references to the Employee objects that represent their
subordinates, keyed by EmployeeID. So, the Employee objects are distributed
among a number of Subordinate collections.

Here's my problem: I need to get to any Employee object in the hierarchy,
based solely on its EmployeeID. I won't have a complete key path down the
hierarchy to the object. What's the best way to do this?

The obvious solution to me is to create another dictionary that contains
object references to all Employee objects, which I would use to access
employees by ID number. But is there a better solution? Thanks.


No, that's absolutely the right way to go.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
Well, you could to a recursive loop

private Employee FindEmployee(int idNumber)
{
Employee emp = SearchEmployees(CEO.Subordinates, idNumber);
return emp;
}

private Employee SearchEmployees(Subordinates sub, int id)
{
foreach(Employee CurrentEmployee in CEO.Employees)
{
if(CurrentEmployee.ID == id)
return CurrentEmployee;
if(CurrentEmployee.Subordinates.Count > 0)
{
Employee check = SearchEmployees(CurrentEmployee.Subordinates,
idNumber);
if(check != null)
return check;
}
}
return null;
}

I haven't tested this, and since an employee can have another employee
subordinate, which in turn could have the first employee as its
subordinate (rather odd, but programmatically legal) you run the risk of
an eternal loop.

--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Nov 15 '05 #3
I did realize that I can probably use an ArrayList, rather than a
dictionary, for the Subordinates property, since I won't be looking up
subordinates by key.
Nov 15 '05 #4
Hmmm... So the benefit would be that I wouldn't need extra dictionaries, and
the disadvantage would be the overhead involved in the search. I suppose
that if I access employees by ID rarely, the recursive search makes the most
sense (avoid the hassle of maintaining an extra dictionary). But if I access
frequently by ID, the extra-dictionary approach makes the most sense (faster
access).

Nov 15 '05 #5

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

Similar topics

7
by: Steven T. Hatton | last post by:
"Reduced subclassing. Factory Method (107) often produces a hierarchy of Creator classes that parallels the product class hierarchy. The Prototype pattern lets you clone a prototype instead of...
0
by: Terentius Neo | last post by:
I have binded a DataGrid to a custom hierarchy. It is an ArrayList of objects. Each object has a public property returning a new ArrayList of sublevel objects (which may again have sublevels of...
1
by: Garris, Nicole | last post by:
We've actually implemented this kind of thing in a different DBMS. The physical design consists of a single "organization" table that's something like: Org_key (primary key) Org_type (group,...
0
by: sonu | last post by:
I have following client side code which i have used in my asp.net project SummaryFeatured Resources from the IBM Business Values Solution Center WHITEPAPER : CRM Done Right Improve the...
1
by: Jeff Lynch | last post by:
I'd like to add the following attributes to the Login control's Login Button to create a CSS rollover effect. How can I access the control's login button from the page's code-behind? ...
4
by: tshad | last post by:
Is there a way for a User Control to access an object (such as label or textbox) on the .aspx page that calls it? For example: x.aspx ************************************** .... Sub...
21
by: George Exarchakos | last post by:
Hi everyone, I'd like your help... Can we have a std::list<BASEwhere BASE be the base class of a class hierarchy? I want to add to this list objects that are inherited from BASE class but not...
14
by: Jo | last post by:
Hi, Is there a generic way to access the (virtual) functions in the base class of a class? Like in: if (!this->Foo(something)) return(((Base*)this)->Foo(something)); // for a normal member...
6
by: Rajib | last post by:
If I have the following code class B { public: int b; }; class C : public B {}; class E : public C, public B {};
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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,...

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.