Font.GetHashCode() problem

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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?
 
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.
 
Back
Top