Thursday, March 3, 2011

Setting a GUID to Empty in the Visual Studio 2010 Watch window

This is something that I stumbled upon the other day and I’ve finally gotten a chance to write it up. My current client has an application that uses GUIDs for IDs and there are lots of IDs. There is also a lot of logic in the application that is based on the presence of absence of a GUID value. The absence is indicated by the use of the Empty GUID.

So the other day a co-worker and I were using the debugger to delve into the bowels of the app trying to find the cause of a defect. We came to the conclusion that there was an issue with state around one of the GUIDS. The GUID had a value when it was supposed to be Empty. Luckily this is easy to test since there are a number of way to alter the value of a variable within the debugger.

Unfortunately it took a bit to discover how to set a GUID to empty.

It seems that you can’t just use the value “{00000000-0000-0000-0000-000000000000}”. You’ll be punished with an error message stating “Invalid expression term ‘{‘”. Your next thought might be to remove the braces ({}). In that case you’ll be rewarded with another error message stating “Cannot convert type ‘int’ to ‘System.Guid’.

I was shocked to find out that the answer is to enter ‘Guid.Empty’ for the value:

SNAGHTMLab00d12

Results in:

SNAGHTMLab2217c

1 comment:

  1. ..and to assign it to a particular GUID, you need to use Guid.Parse(string) method, where the input arg is the string representation of the GUID you want to assign your variable to.

    ReplyDelete