Archive for July, 2004

 

Debug symbols with Visual Studio 2005

Jul 28, 2004 by Graeme in Development

Here’s how to enable automatic downloading of the debug symbols with Visual Studio 2005 Beta 1.

  1. Open up the Options window from the Tools menu
  2. Expand Debugging and select Symbols
  3. Add “http://msdl.microsoft.com/download/symbols” to the list of locations (and make sure it’s enabled)
  4. If you have other developers on your LAN who want to use the symbols, set the cache directory to some network folder so that you don’t all download the symbols again and again

Assuming that it works (it’s a beta, after all), that should be all that’s required. Much easier than in previous versions!

Is properly overriding Equals (and therefore GetHashCode) in .NET classes impossible?

Jul 28, 2004 by Graeme in Development

I just don’t get the rules concerning
System.Object
’s
GetHashCode
and
Equals
methods. Here they are, taken from the current
.NET Framework Class Library Reference
documentation (I’m actually using 2.0,
but this stuff doesn’t change):

Notes to Implementers: A hash function is used to quickly generate a number
(hash code) that corresponds to the value of an object. Hash functions are usually
specific to each Type
and must use at least one of the instance fields as input. A hash function
must have the following properties:

  • If two objects of the same type represent the same value, the hash function must
    return the same constant value for either object.

  • For the best performance, a hash function must generate a random distribution for
    all input.

  • The hash function must return exactly the same value regardless of any changes that
    are made to the object.

My problem with the above is this: The emphasised text in the above statement,
together with Rule 1 and Rule 3 mean that all objects must be immutable, like href="http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemstringclasstopic.asp">
System.String (which is - rather conveniently - the example given in the documentation).
If my GetHashCode method must satisfy the first rule whilst using at least one of
the instance fields, then the return value must change if the value of the instance
field(s) changes. Rule 3 says it can’t.

Here’s one of the example implementations from the documentation:

public struct Int32 {
   public int value;

   //other methods...

   public override int GetHashCode() {
      return value;
   }
}

This obviously passes the “must use a field” rule, but fails Rule 3. The other examples
have the same problem.

I plucked a few random classes out of the BCL and looked at their Equals and GetHashCode
implementations using Reflector.
They all implemented Equals just the way you’d expect, but I thought the GetHashCode
implementations were interesting:


  • System.Net.Cookie
    : Generates a new value from the current state of the fields, violating
    Rule 3.

  • System.Drawing.Drawing2D.Matrix
    : Cheat, cheat, cheat!! You know the compiler warning
    you get when you override Equals but not GetHashCode? This class overrides GetHashCode,
    but all it does is call base.GetHashCode(), cunningly avoiding the compiler warning
    but violating Rule 1!

  • System.Uri
    : Returns a the value of a field called m_Hash,
    which is calculated in the constructor and then isn’t changed.

The first two are clearly in violation of the rules, but I thought System.Uri’s
implementation was interesting. So it uses the fields to calculate the hash, but
then you can freely alter the fields via the object’s properties and the hash doesn’t
change. OK. But what if you try to look up a Uri in a hashtable? Wouldn’t this lead
to a failure to find a Uri that had been inserted in the hashtable and then had
its fields altered? I don’t know, and my brain is beginning to hurt.

Does anybody have a set of unit tests (NUnit preferred) to exercise the Equals and
GetHashCode methods? Has anyone created a class that would pass these tests? I understand
what GetHashCode is used for, but that doesn’t help solve the problem. How are we
supposed to know if our class is to be used as a key in a hashtable?


Update: System.Uri is immutable (thanks, Brad), so that means the hashtable problem disappears. It doesn’t help me implement my mutable classes though!

Update: I just found a similar rant by Ian Griffiths. He concludes:

The right thing to do would be to document this hash constancy requirement as a feature of the
Hashtable rather than trying to push it into the GetHashCode requirements. In fact
that’s precisely what the documentation for Hashtable does! It says “Key objects must be
immutable as long as they are used as keys in the Hashtable.”
So it looks like the persistent impossible advice in GetHashCode is simply historical accident.

I agree - let’s see the documentation fixed!

iPAQ Users Start Windows Mobile SE Petitions

Jul 27, 2004 by Graeme in Development

It seems that those sneaky folks at HP announced that they wouldn’t be providing upgrades to Windows Mobile 2003 Second Edition for existing iPAQ users while I was on holiday!

Brighthand: People who are unhappy about HP’s decision to forgo all Windows Mobile 2003 Second Edition upgrades have found several ways to express their displeasure.

I don’t feel the need to express my displeasure; I’m sure they’ve got the message already. :(

Lose your privileges

Jul 26, 2004 by Graeme in Development

I know I shouldn’t develop as an administrator, but I still do. Eric points at some tools which might help me make the switch. Someday. ;)

Football, with feet

Jul 26, 2004 by Graeme in Fun

Here are some interesting thoughts on last weekend’s Chelsea/Celtic match held somewhere in the United States.

One thought occurred to me; wouldn’t Nottingham, say, have made a more convenient venue? :)

Life without Google?

Jul 26, 2004 by Graeme in Uncategorized

At the time of writing, I’m getting this back for all Google searches:

Google    
Error
 

Server Error

The service you requested is not available at this time.

Service error -27.

Get well soon, Google!

Update: Looks like this is a Google UK problem. More info from BBC News.

Update 2: OK, OK, it wasn’t just the UK that was affected. Turns out it was the latest MyDoom incarnation. Plenty of coverage elsewhere which I won’t repeat. I was just a bit spooked having never seen Google not working before.

Say it ain’t so!

Jul 20, 2004 by Graeme in Development

I can’t see a way to build an application to run on version 1.1 of the .NET Framework using Visual Studio 2005! Don’t tell me I’ll have to go back to Visual Studio 2003 for stuff I want to release this year (i.e. a new version of FetchLinks)?

Don’t take my class diagrams away! Noooooooooo!

NUnitAddin for Visual Studio 2005 Beta 1

Jul 19, 2004 by Graeme in Development

Despite the hard work required, Jamie has released a new build of NUnitAddin which works with the Beta 1 build of Visual Studio 2005 (including the Express versions) as well as the older versions. Go get!

Sunny Clacton-on-Sea

Jul 09, 2004 by Graeme in Family

We’re off to “sunny” Clacton-on-Sea tomorrow for a bit of a holiday, so I’m not going to be posting for a while.

You might have noticed I’ve enabled unmoderated, anonymous comments so there could be a bit of comment spam to be cleared up when we get back. I’d like to apologise in advance for anything offensive that gets posted in my absence. All I can say in my defence is “it wasn’t me!” ;)

Fixing FireFox

Jul 08, 2004 by Graeme in Uncategorized

Here are a couple of problems which I’m bound to need the solutions to again:

  • Each time I double-click a URL shortcut on my desktop, Firefox loads the URL, but the following message pops up - “Windows cannot find (add any web url here). Make sure you typed the name correctly, and then try it again. To search for a file, click the Start button, and then click Search.” Solution
  • When I click on a link in Outlook, the link opens up, but also a “Locate Link Browser” box pops up. Solution (seems to be down at the moment), Another Solution