473,486 Members | 2,353 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Hashtable Key objects

Hi,

I am using a System.Windows.Forms.Label object as key in my Hashtable. One
thing that has surprised me is that if I create two Label objects with same
properties (ie. Name, TabIndex,Text,Size,Dock) and use them as Key in my
Hashtable it doesn't give any error.

I thought, generally, in a Hashtable we cannot use two same objects as keys.
The Label Objects in this case are being created in a loop and used as keys
in a Hashtable.

Can anyone please shed some light on this.

Regards,

Wamiq Ansari
wa***@msn.com
Nov 16 '05 #1
2 2576
Wamiq,

By default, the hash of value types is based on the contents of the
structure, so creating a new structure, setting the values to the same
thing, and then using that as a hash will give you what you wanted.

However, for reference types, by default, the hashcode is based on the
reference equality, which is why you are seeing what you are seeing.

You have some options here. First, you can provide your own hash code
provider, and not use the GetHashCode implementation on the Label class
(which defers to Object.GetHashCode anyways), or, you could create a
structure that has the properties of the label you want to use as your key.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Wamiq Ansari" <wa***@msn.com> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
Hi,

I am using a System.Windows.Forms.Label object as key in my Hashtable. One
thing that has surprised me is that if I create two Label objects with same properties (ie. Name, TabIndex,Text,Size,Dock) and use them as Key in my
Hashtable it doesn't give any error.

I thought, generally, in a Hashtable we cannot use two same objects as keys. The Label Objects in this case are being created in a loop and used as keys in a Hashtable.

Can anyone please shed some light on this.

Regards,

Wamiq Ansari
wa***@msn.com

Nov 16 '05 #2
Hi, Wamiq

it should not surprise you, because when you create 2 objects they are
different, even if seemingly properties are same. You can see that by trying
if(label1==label2) and checking result.
Or you can check that label1.Handle!=label2.Handle.

I would suggest to check chapter on reference types in C# Language Reference
to get all the details.

HTH
Alex

"Wamiq Ansari" <wa***@msn.com> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
Hi,

I am using a System.Windows.Forms.Label object as key in my Hashtable. One
thing that has surprised me is that if I create two Label objects with same properties (ie. Name, TabIndex,Text,Size,Dock) and use them as Key in my
Hashtable it doesn't give any error.

I thought, generally, in a Hashtable we cannot use two same objects as keys. The Label Objects in this case are being created in a loop and used as keys in a Hashtable.

Can anyone please shed some light on this.

Regards,

Wamiq Ansari
wa***@msn.com

Nov 16 '05 #3

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

Similar topics

5
7708
by: Arjen | last post by:
Hello, Let's say that we have a hashtable with some person objects. This persons have a name. Now I want to sort the people objects inside the hashtable by name. How can the hashtable do this...
4
8339
by: Anders Borum | last post by:
Hello! I have a list of singleton classes (model managers) that store objects internally using hashtables. Each of these classes use a single hashtable to store e.g. users, pages, elements and...
3
1275
by: A. Burch | last post by:
I'm wanting to use a Hashtable. I have 2 (keys, objectvalues) pairs of data to store. I'm using a static declartion for the Hashtable the instance of the class that is the object. If I try and...
33
3284
by: Ken | last post by:
I have a C# Program where multiple threads will operate on a same Hashtable. This Hashtable is synchronized by using Hashtable.Synchronized(myHashtable) method, so no further Lock statements are...
8
1675
by: Robin Tucker | last post by:
When I create a hashtable hashing on Object-->Item, can I mix "string" and "integer" as the key types? I have a single thumbnail cache for a database with (hashed on key) and a file view (hashed...
7
1683
by: J L | last post by:
I have defined a structure private structure FieldInfo dim FieldName as string dim OrdinalPostioin as Integer dim DataType as Type dim Size as Integer end structure I read this information...
2
3496
by: Scott M. Lyon | last post by:
I'm working on using the COM Interop wrapper on an existing .NET library, to allow existing VB6 applications to call the .NET code. And I've been able to get it working in a basic sense (I can...
7
2520
by: SevDer | last post by:
Hi We have a static hashtable that is located in another tier in our n-tiered web application. And we are storing big but not huge objects in this hashtable and the key to the objects is...
10
8484
by: chrisben | last post by:
Hi, Here is the scenario. I have a list of IDs and there are multiple threads trying to add/remove/read from this list. I can do in C# 1. create Hashtable hList = Hashtable.Synchronized(new...
0
6967
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6846
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7341
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5439
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4870
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4564
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3071
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1381
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
600
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.