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

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_ClearanceLevel;
}

// 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 1371
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_ClearanceLevel;
}

// 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.ObjectIDGenerator. 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
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....
9
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...
6
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...
13
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...
29
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...
14
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...
0
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...
12
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...
9
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...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.