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;

4 Comments

Comments have been disabled for this content.