473,770 Members | 1,994 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Class design issue: how to store unique inherited objects?

I have an unfinished small class hierarchy that looks something like the
code snippet below. The intent is to have a simple company identification
system that ensures uniqueness by calling IsUnique() and allows for
variations between different types of ID. (This isn't at all close to what
I'm actually coding, just a good approximating example.)

[begin code snippet]
abstract class PersonID {
public string m_Name;
public int m_ID;
public static bool IsUnique(m_ID) { ... }
}

class EmployeeID : PersonID {
public int m_Keycode;
}

class SupervisorID : PersonID {
public int m_ClearanceLeve l;
}

// several other PersonID-derived types ...
[end code snippet]

I have two issues:

(1) When a new PersonID or derived class is instantiated, I have to
guarantee that the ID generated by it is unique. The naive easy way is to
have a static counter increment in the constructors and to assign this value
to m_ID, but that seems unaesthetic. I'm leaning towards just using random
numbers, I think, but if anyone has a better idea I'd love to hear it.

(2) To completely guarantee uniqueness, I'd need to remember all the
previously assigned IDs and make sure the one in question hasn't been used
yet. This involves some kind of storage; a custom hashtable seems to suggest
itself. Where should this go? I'm thinking that some kind of IDManager class
might do the trick, perhaps along the lines of:

[begin code snippet]
// revised PersonID
class PersonID {
// other stuff ...
public static IDManager mgr;
}

public IDManager {
// strongly-typed custom collection
public IDHashtable IDTable;
}
[end code snippet]

(3) However, if I do something like that described in (2), there's no way
for me to tell which of the many PersonID types the object stored in the
Hashtable is without checking against each of the types in turn (e.g., "if
(obj is EmployeeID) { ... } else if (obj is SupervisorID) { ... } ...").
This involves some kind of nasty switch/case statement or if/else block and
the "is" keyword, I presume. Is there a good way to do this that I'm missing
here?

Any help is greatly appreciated. Thanks!

- JJ
Nov 16 '05 #1
1 1394
Hi JJ,

See my comments inlined
[begin code snippet]
abstract class PersonID {
public string m_Name;
public int m_ID;
public static bool IsUnique(m_ID) { ... }
}

class EmployeeID : PersonID {
public int m_Keycode;
}

class SupervisorID : PersonID {
public int m_ClearanceLeve l;
}

// several other PersonID-derived types ...
[end code snippet]

I have two issues:

(1) When a new PersonID or derived class is instantiated, I have to
guarantee that the ID generated by it is unique. The naive easy way is to
have a static counter increment in the constructors and to assign this value to m_ID, but that seems unaesthetic. I'm leaning towards just using random
numbers, I think, but if anyone has a better idea I'd love to hear it.
Frankly, I don't see it unaesthetic at all.
(2) To completely guarantee uniqueness, I'd need to remember all the
previously assigned IDs and make sure the one in question hasn't been used
yet. This involves some kind of storage; a custom hashtable seems to suggest itself. Where should this go? I'm thinking that some kind of IDManager class might do the trick, perhaps along the lines of:

[begin code snippet]
// revised PersonID
class PersonID {
// other stuff ...
public static IDManager mgr;
}

public IDManager {
// strongly-typed custom collection
public IDHashtable IDTable;
}
[end code snippet]
Isn't it the same as having static counter?
Anyways if you don't want to persist those ID's you can take a look at
System.Runtime. Serialization.O bjectIDGenerato r. This is the same class used
by the framework when generating object graphs. Bare in mind that the ID's
are unique in the context of one concrete instance of this generator.

(3) However, if I do something like that described in (2), there's no way
for me to tell which of the many PersonID types the object stored in the
Hashtable is without checking against each of the types in turn (e.g., "if
(obj is EmployeeID) { ... } else if (obj is SupervisorID) { ... } ...").
This involves some kind of nasty switch/case statement or if/else block and the "is" keyword, I presume. Is there a good way to do this that I'm missing here?


I didn't get that. ID is porperty of the base class. Why do you need those
if-else-ifs?
--
HTH
Stoitcho Goutsev (100) [C# MVP]

Nov 16 '05 #2

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

Similar topics

13
2387
by: Bryan Parkoff | last post by:
I have created three classes according to my own design. First class is called CMain. It is the Top Class. Second class and third class are called CMemory and CMPU. They are the sub-classes. Two sub-classes have the relationship to communicate back and forth through this pointer. The pointer is responsible inside Top class for allocating and deallocating two sub-classes. CMemory class is responsible to allocate and deallocate memory...
9
4806
by: justanotherguy63 | last post by:
Hi, I am designing an application where to preserve the hierachy and for code substitability, I need to pass an array of derived class object in place of an array of base class object. Since I am using vector class(STL), the compiler does not allow me to do this. I do realize there is a pitfall in this approach(size of arrays not matching etc), but I wonder how to get around this problem. I have a class hierachy with abstract base...
6
2437
by: Jon Slaughter | last post by:
Is there any way to get the class type from a pointer? I'm working on a class that acts as a set: I have two classes, one called FSet and the other called Element. FSet inherets Element and has an internal pointer to a list of pointer of Element type.
13
1433
by: Scott Meddows | last post by:
I'm writing an NT service to provide security information enterprise wide. I would like to have all of this information cached memory if a previous request loaded it into memory. Then using a common interface I would be able to invalidate and unload old information as it is updated (Removing the item from the collection?) My big question is what collection class will give me the greatest flexibility and greatest performance? I hope...
29
3577
by: MP | last post by:
Greets, context: vb6/ado/.mdb/jet 4.0 (no access)/sql beginning learner, first database, planning stages (I think the underlying question here is whether to normalize or not to normalize this one data field - but i'm not sure) :-) Background info:
14
2644
by: lovecreatesbea... | last post by:
Could you tell me how many class members the C++ language synthesizes for a class type? Which members in a class aren't derived from parent classes? I have read the book The C++ Programming Language, but there isn't a detail and complete description on all the class members, aren't they important to class composing? Could you explain the special class behavior in detail? Thank you very much.
0
2510
by: YellowFin Announcements | last post by:
Introduction Usability and relevance have been identified as the major factors preventing mass adoption of Business Intelligence applications. What we have today are traditional BI tools that don't work nearly as well as they should, even for analysts and power users. The reason they haven't reached the masses is because most of the tools are so difficult to use and reveal so little
12
3120
by: titan nyquist | last post by:
I have a class with data and methods that use it. Everything is contained perfectly THE PROBLEM: A separate thread has to call a method in the current instantiation of this class. There is only ever ONE instantiation of this class, and this outside method in a separate thread has to access it. How do i do this?
9
1569
by: DBC User | last post by:
I am trying to find the difference between set and class. Could some one help me? I can understand that a class is like a set. Each subset could be a inherited class and members are instances. What is the difference between class and set and if there is any? Thanks.
0
9618
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
9454
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,...
0
10259
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10101
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8933
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...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
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
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2849
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.