Collection Quick Access

  • Thread starter Thread starter MFRASER
  • Start date Start date
M

MFRASER

Ok I have a collection that inherits from the CollectionBase. Problem is
that I want a quick way to access the objects either by ID or Name.

I was thinking of creating two hashtables that are held privately by the
collection and when a user calls the following functions it would go to the
appropiate collection to locate it. My question is, is there a better way
to do this?

FindobjectbyID(int ID)
{
return HashID[ID];
}

FindobjectbyName(string Name)
{
return HashName[Name];
}
 
Hi,

I would use two hashtables. This halves the load on each hashtable, making
rehashes less necessary.
Moreover, hashfunctions (Object.GetHashCode) are defined per type, trying to
get the best spreading of the data over the buckets.
Two different kind of keys *might* interfere with each other, resulting
(possibly) in less good spreading and more rehashes.
Just a thought, I have no hard numbers on this particular situation.

Regards,
Bram.
 
I was under the impression that you know both the ID and the name of the object. I was thinking lik

Hashtable table = new Hashtable()
int TestObj = 1
string name = "SomeName"
table.Add(1, TestObj)
table.Add(name, TestObj)

Then, later you can have

public object GetObject(int ID

return table[ID]


public object GetObject(string name

return table[name]


Tu-Thac


----- Fraser Michael wrote: ----
I am sorry but I am missing something. Here is some sample code to ad
an object to a Hashtabl

System.Collections.Hashtable aHash = new Hashtable()
int TestObj = 1
aHash.Add(1,TestObj)

I don't see how to add multiple keys to the same hashtabl



*** Sent via Developersdex http://www.developersdex.com **
Don't just participate in USENET...get rewarded for it
 
Back
Top