473,324 Members | 2,417 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,324 software developers and data experts.

Compare values from objects stored in Hashtable

Hi!

I'm kinda stuck here.
I have an Hashtable with with my custom "Item" Object. The key used in
the Hashtable is the "Item.Id", i have selected this key because with
a lot of functions i can get the right object from the hashtable
really fast. But now i need a function whom can get an object from the
hashtable based on the values each "Item" object holds.
("Item.ColorId","Item.SizeId","Item.OtherId"), but i can't identify
the object by its key (the "Item.Id").

Since it is an hashtable i can't loop through each instance and then
cast and compare the values like you could do with an arraylist. Has
someone got an idea ?

Best Regards,
Frank
Nov 16 '05 #1
3 5852
When you say you can't loop through the Hashtable do you mean because of how
inefficient a linear search is? It is possible to iterate through it:

foreach(Item anItem in myHashtable)
{
if(anItem.ColorId == someColor)
...
}

--Liam.
"Frank" <f.**********@chello.nl> wrote in message
news:80**************************@posting.google.c om...
Hi!

I'm kinda stuck here.
I have an Hashtable with with my custom "Item" Object. The key used in
the Hashtable is the "Item.Id", i have selected this key because with
a lot of functions i can get the right object from the hashtable
really fast. But now i need a function whom can get an object from the
hashtable based on the values each "Item" object holds.
("Item.ColorId","Item.SizeId","Item.OtherId"), but i can't identify
the object by its key (the "Item.Id").

Since it is an hashtable i can't loop through each instance and then
cast and compare the values like you could do with an arraylist. Has
someone got an idea ?

Best Regards,
Frank

Nov 16 '05 #2
No, you usually can't iterate through each object in a Hashtable that
way, since it contains objects, not specific types (you'll throw
exceptions left and right if you try).

Instead:
foreach (string sKey in myHashtable.Keys)
{
if(myHashtable[sKey].ColorId == someColor)
...
}

*-----------------------*
Posted at:
www.GroupSrv.com
*-----------------------*
Nov 16 '05 #3
Ack - that's not quite right either - you need to cast to an "Item"
object.

This is what you need:

foreach (string sKey in myHashtable.Keys)
{
if (((Item)myHashtable[sKey]).ColorId == someColor)
...
}

*-----------------------*
Posted at:
www.GroupSrv.com
*-----------------------*
Nov 16 '05 #4

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

Similar topics

0
by: John Meredith via .NET 247 | last post by:
Hi all, I used all your info to come up with the following conclusion when I needed to modify my Hashtable object. 1) Create an ArrayList from the Hashtable keys so that we have something to...
0
by: Ricardo Pereira | last post by:
Hi, I wrote a simple program to test the "out-of-the-box" functionalities of the CMAB (Configuration Management Application Block). After having added 2 values to an hashtable, and having written...
122
by: Einar | last post by:
Hi, I wonder if there is a nice bit twiddling hack to compare a large number of variables? If you first store them in an array, you can do: for (i = 0; i < n; i++) { if (array != value) {...
5
by: Mountain Bikn' Guy | last post by:
How would I do this? public sealed class UtilityClass { public static MyObject Object1;//see note below about importance of static object names in this class public static MyObject Object2;...
8
by: SenthilVel | last post by:
how to get the corresponding values for a given Key in hashtable ??
1
by: Mark | last post by:
by m.r.davies I have 2 tables on seperate Db's (and servers) I want to use a datareader on the first table to pick the booking ref, and then use that booking ref to query the 2nd DB when i have...
3
by: Sam | last post by:
Hi Everyone, I have a stucture below stored in an arraylist and I want to check user's input (point x,y) to make sure there is no duplicate point x,y entered (string label can be duplicated). Is...
4
by: Lamis | last post by:
Hi, what is the best way to compare 2 haschtables contatining objects. the objects has 2 property, name & value. I need to print out the differences -- LZ
5
by: RSH | last post by:
I have two HashTables (_CompanyDeductions,_CompanyAccruals) that contain several objects each. I am trying to loop through and print the properties of the objects in the Hashtables: I am trying...
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: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
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: 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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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.