Thursday, August 02, 2007

Using the Immediate Window in VS to Explore APIs

The Immediate Window in Visual Studio can be a great help if you’re trying to grok a particular API, or do a quick spike/prototype to understand behavior of a class, method, or property. The Immediate Window lets you make API calls, check values of variables, and even do assignments.  It’s a handy way to explore how your program’s behaving and how things might work if you alter data or calls.

For example, the documentation for the bits and pieces of the highly useful Uri and VirtualPathUtility classes often don’t include examples of what’s returned from various calls.  This is a hinderance as I’m trying to get formatting of file-based resource URIs correct to play with the WebBrowser control I’m using.  I need to do two things: 1) ensure the format of the URI is correct when I assemble it in the actual <link> element in the content source, and 2) extract a complete absolute path to the resource without the protocol part when I am doing my navigation.

I can write a prototype app with a class full of a bunch of crappy Console.Writeln statements to see the before/after bits. This isn’t much fun, even if I use Snippet Compiler.  On the other hand, I can start my existing project in VS in debug mode with a breakpoint set appropriately, then play around in the Immediate Window to discover the exact format of the URI and which API call I need to make.

For example, say my input Uri is a variable called “resource” which will point to a file-based resource. My starting content for that might be “file://Temp/Test.pdf”.

I flip to the Immediate window and enter


which results in


Not what I wanted since it’s missing the Temp directory.  Maybe I can look at Uri.LocalPath:



Still not right, since the Temp folder is showing up as location component.  Let me alter the actual Uri variable “resource”:

resource = new Uri("file://c:/Temp/Test.pdf")

You’ll see a whole bunch of stuff fly by as all the new Uri’s properties are displayed.  Now let’s check the LocalPath property again:




All this is pretty simplistic use of the Immediate Window, but it shows a different aspect than it’s normally used for.

1 comment:

Steven Harman said...

Sure would be nice to have the equivalent of Ruby's rib, wouldn't it. Then you wouldn't even need to set a breakpoint... just start firing off commands and building out your objects.

Oh, we can dream can't we?

Subscribe (RSS)

The Leadership Journey