Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way I can make VS2010 expand or contract try blocks inside my methods?

Tags:

c#

My code has a lot of try, catch, finally blocks. Unlike methods when I am in VS2010 I can't expand or contract these areas to hide the contents while developing except by adding regions.

        try {
            vm.R...
            vm.Qu..
            vm.T...
            vm.D...
            vm.Fil..
            vm.Type..
            vm.St..
        } catch (Exception e) {
            log(e);
            return Content(ExceptionExtensions.GetFormattedErrorMessage(e));
        }

Is there a technique that programmers use to tidy up code within try blocks or do I just have to live with these larger blocks of code.

like image 966
Alan2 Avatar asked Sep 13 '12 04:09

Alan2


People also ask

How do you expand all in VS code?

Fold All (Ctrl+K Ctrl+0) folds all regions in the editor. Unfold All (Ctrl+K Ctrl+J) unfolds all regions in the editor. Fold Level X (Ctrl+K Ctrl+2 for level 2) folds all regions of level X, except the region at the current cursor position.

How do I expand all methods in Visual Studio?

Expand / Close All Where you see braces or regions in code, you can collapse or expand them with the keyboard shortcut Ctrl + M, P to expand or Ctrl + M, O to collapse.

How do I expand and collapse Methods in Visual Studio?

CTRL + M + O will collapse all. CTRL + M + P will expand all and disable outlining. CTRL + M + M will collapse/expand the current section.

How do I expand all regions in Visual Studio?

Not available in Visual Basic. (Ctrl+M, Ctrl+M) - Reverses the current hidden or expanded state of the innermost outlining section when the cursor lies in a nested collapsed section. (Ctrl+M, Ctrl+L) - Sets all regions to the same collapsed or expanded state.


2 Answers

Without actually modifying the code, the only thing I know of is to use outlining. You can "hide" a selection which basically creates an outline section that you can expand and collapse just like a class, namespace, method, #if block, etc. To do this:

  • Select the lines you want to collapse.
  • Choose Edit\Outlining\Hide Selection (or Ctrl+M,Ctrl+H with the C# keyboard scheme)

The selection is now collapsed and you can expand it and re-collapse it any time you want just like a method. (e.g. with +/- glyph at the left or with Ctrl+M,M)

like image 153
Peter Ritchie Avatar answered Oct 16 '22 11:10

Peter Ritchie


For really clean code, try Extracting Methods, so that your blocks are smaller. And, if it's really necessary, you'll get the automatic code folding from Visual Studio.

like image 43
Mike Stockdale Avatar answered Oct 16 '22 11:10

Mike Stockdale