Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / May 2005

Tip: Looking for answers? Try searching our database.

when is hashcode used?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Saurabh - 09 May 2005 17:13 GMT
Hello All,

I have been facing this bizarre problem within a combobox having a list of
UK postcodes. When I assign the SelectedItem propoerty of the combobox, a
completely different item was actually selected in the combobox. I broke
into the code at the assignment statement and then looked at the values for
different items in the watch window. It looked something like this :

postcodeObj : EH11 1LQ
comboBox1.SelectedIndex = -1 (nothing currently selected)
comboBox1.Items.IndexOf(postcodeObj) : 1614
comboBox1.Items[1614].ToString() : EH11 1LA (something different to what i
am intending to set as selected item)

I also found out that my item was at the location 1621 in the combobox
items.

The combobox is no sorted, and I couldn't figure out what was going wrong
here, till I added 2 more things in the watch window
comboBox1.Items[1614].GetHashCode( )
comboBox1.Items[1621].GetHashCode( )

These 2 values appeared to be same, Now I remember that I have overriden the
GetHashCode() method in my postcode object to return a hashcode, it creates
a hashcode based on the postcode string and one more attribute. (XOR the
number and the hashcode of the string)

My question is, Is hashcode used internally by the 'SelectedItem' set
method? Is there any way for me to get round this problem? I have the
postcode strings and a unique number, Can I generate a unique hashcode from
this combination? I don't want the number to be my hashcode.

TIA,

--Saurabh
Lloyd Dupont - 10 May 2005 04:33 GMT
I believe internaly they used Equals().
You can generate hashcode the way you like, but hascode are mostly relevant
with immutable object.

> My question is, Is hashcode used internally by the 'SelectedItem' set
> method? Is there any way for me to get round this problem? I have the
[quoted text clipped - 4 lines]
>
> --Saurabh
Saurabh - 10 May 2005 09:05 GMT
Aaaaaaah, so it means if I override Equals( ), thats where I need to see
whats going wrong.
Thanks for the tip,

--Saurabh
>I believe internaly they used Equals().
> You can generate hashcode the way you like, but hascode are mostly
[quoted text clipped - 8 lines]
>>
>> --Saurabh

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.