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