How to set a string to null using the Watch Window in Visual Studio .NET 2003

An interesting customer question came my way about how to set a string to null in the Watch Window in Visual Studio .NET 2003.  Note: this issue does not reproduce in Visual Studio 2005.  If you try to set a string to null in the Watch Window, Visual Studio will just ignore you and will reset the value back to the original.  If you try to be more forceful and use the Quick Watch window, you’ll get an error saying, “The value of this variable could not be changed. Incorrect function.”

Thanks to Ben on the C# team for the workaround:

  1. Add the string (for example, named "str") to the watch window 
  2. Change the Name (in the "Name" column) to (object)str to cast it into an object 
  3. Click the + sign to expand the entry in the watch window 
  4. In the row that has Name "string", set the value to null 
  5. Now you can set the Name back to str if you want to

Looking for Visual Studio .NET 2003 Tips and Tricks? – here’s a book for you

Minh Nguyen wrote a collection of Tips and Tricks for 2002 and 2003, with some 2005 Beta 1 tips too.   The book’s called Visual Studio .NET Tips and Tricks.

The book covers

  • Editor and other IDE tips
  • Compiling and Debugging tips
  • Miscellaneous .NET tips and tricks

Many of the generic tips (like Editor and other IDE tips) apply to 2005.  I’m always collecting tips and tricks for this feed from my team and what other bloggers (both MS and customers) are offering as tips, so this book would be a good supplement if you enjoy reading this feed.

Happy Visual Studio’ing!