473,549 Members | 2,655 Online
Bytes | Software Development & Data Engineering Community
+ 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,S ize,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 2582
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.GetHashC ode 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.co m

"Wamiq Ansari" <wa***@msn.co m> wrote in message
news:%2******** *******@TK2MSFT NGP10.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,S ize,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==labe l2) 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.co m> wrote in message
news:%2******** *******@TK2MSFT NGP10.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,S ize,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
7716
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 for me? Thanks!
4
8345
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 so on (I have complete control of the objects stored). I am currently using a set of abstract cache classes that my model managers subclass. The...
3
1278
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 retreive the first object after storing after the 2 (key, objectvalues) are stored I always get the last object value stored. Why doesn't qrec =...
33
3294
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 used before adding, removing or iterating the Hashtable. The program runs in a high workload environment. After running a few days, now it suddenly...
8
1679
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 on string). So, for example, when I want to know if the file "xyz.abc" is in the cache, I can write myTable.ContainsKey ( "xyz.abc" ) or if a given...
7
1694
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 from a DataReader which retrieves schema info
2
3500
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 return basic types like strings and booleans, and I've added a reference to MSCORLIB.tlb so I can even return Hashtables, which the .NET library...
7
2528
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 through Session.SessionID. public class DataStore : System.Web.UI.Page { public static Hashtable ht = new Hashtable();
10
8488
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 Hashtable()); 2. create ArrayList aList = ArrayList.Synchronized(new ArrayList()); 3. create a string sList = ""; For 1 and 2, since the list is...
0
7541
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7464
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7734
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7497
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6065
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5107
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3493
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1074
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
781
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.