Kevin Dente's Blog

The Blip in the Noise
Simple but handy VS2005 macro for C# code navigation
Frequently while navigating code in VS2005, I want to go to the definition of the type of a variable. For example, say you're looking at a line of code like this:
...
aVariable = DoSomethingThatReturnsOneOfThese();
...

And you want to jump to the source for the type of aVariable (that is, if aVariable is of type SomeClass, you want to jump the source for SomeClass). As far as I can see, Visual Studio doesn't give you a simple command that does that navigation. So here's a simple macro that does it.

   Sub GoToVariableType()
      DTE.ExecuteCommand("Edit.GoToDefinition")
      DTE.ActiveDocument.Selection.WordLeft()
      DTE.ExecuteCommand("Edit.GoToDefinition")
   End Sub

Trivial but effective, and it saves me a couple of keystrokes. I map this macro to CTRL-F12 (a shortcut which as far as I can tell is only used by C++ projects).

Note that this only works if you don't declare multiple variables in one declaration statement. For example, it works with declarations like this:

SomeClass aVariable;

but not this:

SomeClass aVariable, anotherVariable;

Published Friday, March 24, 2006 3:47 PM by kevindente

Filed under:

Comments

# Re: Simple but handy VS2005 macro for C# code navigation@ Friday, March 24, 2006 6:57 PM

F12 does this with the default settings. with the VB settings I think it's SHift-F7..
You can go to tools-options-keyboard and look up that command. you'll see what shorctu it has set to it, and you can of course change it.

Roy Osherove

# re: Simple but handy VS2005 macro for C# code navigation@ Friday, March 24, 2006 7:05 PM

Roy,
F12 goes to the declaration of a variable, not to the definition of that variable's type.

Kevin Dente

# re: Simple but handy VS2005 macro for C# code navigation@ Sunday, March 26, 2006 8:09 PM

Kevin, ah, I get it. One more F12 then.. right?

Roy Osherove

# re: Simple but handy VS2005 macro for C# code navigation@ Friday, March 31, 2006 4:40 PM

Hey, I'm new to to this macro in VS2005 stuff. how do i add it to my IDE? (better yet if you have a link that could explain it)

Justin

Leave a Comment

(required) 
(required) 
(optional)
(required)