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 / August 2005

Tip: Looking for answers? Try searching our database.

Font.GetHashCode() problem

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Markus - 05 Aug 2005 14:04 GMT
I have a little webcontrol that generates images based on propertyvalues. To
get a name for each image that is "unique" for only one combination of
propertyvalues on the control I use GetHashCode from each property and
combines the values I get to create the name (ie 2452566266.gif). That should
give me a name that is always the same as long as the propertyvalues are the
same. As soon as one of the propertyvalues are changed (ie the property
BackgroundColor is changed from "Green" to "White") a new imagename/image is
created (which is exactly what I want).

With me so far?

All this works fine except for my Font property. Let's say
myFontProperty.GetHashCode() generates 123456789 the first time, same number
the second time, same number the third time and so on. But now here's the
funny part - after restarting my computer and running the exact same code
(the font property has the exact same fontname, size etc as before the
restart), myFontProperty.GetHashCode() generates another number. Is this
behavior really correct? Shouldn't a font instance with the exact same values
in it always generate the exact same hashcode?
Jon Skeet - 12 Aug 2005 07:28 GMT
On 5/8/05 2:04 pm, in article
0324FFBF-1D4F-4FEE-A64E-0160B50F3E79@microsoft.com, "Markus"

> I have a little webcontrol that generates images based on propertyvalues. To
> get a name for each image that is "unique" for only one combination of
> propertyvalues on the control I use GetHashCode from each property and
> combines the values I get to create the name (ie 2452566266.gif). That should
> give me a name that is always the same as long as the propertyvalues are the
> same.

Yes, but it's not guaranteed to give you a different long when the property
values are different. It's also not guaranteed to give you the same number
when you rerun the code in a different process - and that's what's giving
you problems. So long as the same actual object gives the same hashcode
throughout its lifetime, and any other object which is equal to it according
to Equals gives the same hashcode (during the lifetime of the process) it's
obeying the contract. That's what you're running into, and it's basically
because you're using hashcodes in a way they're not intended for.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


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.