The main reason for header files is to enable separate compilation of files, and minimize dependencies.
Place your caret on the first line of any C# or Visual Basic file. Press Ctrl+. to trigger the Quick Actions and Refactorings menu. Select Add file header. To apply the file header to an entire project or solution, select Project or Solution under the Fix all occurrences in: option.
In Visual Studio 2013 and later there is a default keyboard shortcut for this: Ctrl+K, Ctrl+O (You will need to hold down Ctrl and type ko and then release Ctrl)
In earlier versions, see:
Visual Studio Macro to switch between CPP and H files
or
Open Corresponding File in Visual Assist
In Visual Studio 2013 a default keyboard shortcut for this is Ctrl+K, Ctrl+O
You could add this macro to your VS config (via Tools -> Macros -> Macro Explorer) then assign a hotkey to it (via Tools -> Options -> Environment -> Keyboard).
I only just wrote it (been meaning to try this for ages!) but it seems to work so far, in both VS2008 and VS2010.
Since it's a macro you can edit it to include whatever rules you want (e.g. looking in other folders, or special naming rules if you have a single header shared by multiple cpp files or similar).
Here's the macro (I'm sure it could be better written; I'm unfamiliar with the VS objects and only realised macros were using .Net about half-way through writing the thing :)):
Sub FileSwitch()
Try
Dim CurrentPath As String = DTE.ActiveDocument.FullName
Dim OtherPath As String
If (IO.Path.HasExtension(CurrentPath)) Then
Dim CurrentExtension As String = IO.Path.GetExtension(CurrentPath)
Select Case CurrentExtension
Case ".h", ".hpp", ".hxx"
OtherPath = IO.Path.ChangeExtension(CurrentPath, ".cpp")
If (Not IO.File.Exists(OtherPath)) Then
OtherPath = IO.Path.ChangeExtension(CurrentPath, ".c")
If (Not IO.File.Exists(OtherPath)) Then
OtherPath = IO.Path.ChangeExtension(CurrentPath, ".cxx")
End If
End If
Case ".cpp", ".c", ".cxx"
OtherPath = IO.Path.ChangeExtension(CurrentPath, ".h")
If (Not IO.File.Exists(OtherPath)) Then
OtherPath = IO.Path.ChangeExtension(CurrentPath, ".hpp")
If (Not IO.File.Exists(OtherPath)) Then
OtherPath = IO.Path.ChangeExtension(CurrentPath, ".hxx")
End If
End If
Case Else
End Select
If (OtherPath <> Nothing) Then
DTE.ItemOperations.OpenFile(OtherPath)
End If
End If
Catch ex As System.Exception
MsgBox(ex.Message)
End Try
End Sub
Here's a (very wide :)) screenshot showing what the macro editor and hotkey/options dialogs should look like, to help those not familiar with them:
Try PhatStudio. It's free and comes with an easy installer.
ALT + S = Switch between header/source file
ALT + O = Open a file (supports instant search via typing, like the start menu in Windows Vista/7).
Try Visual Assist, which sports this very feature (amongst others):
http://www.wholetomato.com/
The code browsing functionality -- of which the header/cpp swap is one part -- are really good.
(I also really rated its intellisense and refactoring features, but not everybody I've spoken to has agreed with me.)
EDIT: just remembered, the Nifty Solution Plugin also does this -- plus another handly Visual Assist-like thing, though nothing else -- and they're free:
http://code.google.com/p/niftyplugins/
(The guy's perforce plugin is great, too. Much better than the default VSSCC rubbish.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With