469,902 Members | 1,949 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,902 developers. It's quick & easy.

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 1260
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

13 posts views Thread by Bryan Parkoff | last post: by
6 posts views Thread by Jon Slaughter | last post: by
13 posts views Thread by Scott Meddows | last post: by
14 posts views Thread by lovecreatesbea... | last post: by
reply views Thread by YellowFin Announcements | last post: by
9 posts views Thread by DBC User | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.