Skip to main content
All docs
V22.2
  • How to: Wrap Multiple Sections into Regions

    • 3 minutes to read

    CodeRush can wrap each member section into a region and surround these member sections with the parent region.

    namespace MyApp
    {
        public class Example
        {
            #region Private fields, private events, and private methods 
            #region Private fields
            object field1;
            object field2;
            #endregion
            #region Private events
            event EventHandler EventName1;
            event EventHandler EventName2;
            #endregion
            #region Private methods
            void MethodName1() { }
            void MethodName2() { }
            #endregion
            #endregion
        }
    }
    

    Follow the steps below to achieve the result shown in the code snippet above:

    1. Open the Editor | All Languages | Organize Members options page to configure the Organize Members feature.

      organize-members-page

    2. In the “Rule set” section, click New to open the “New Set” dialog.

      empty-rule-set

    3. Rename the suggested rule set name, for example, to “My Rule Set”. Click OK to save the rule set name and close the “New Set” dialog.

      rename-to-my-rule-set

      CodeRush creates an empty rule set that you can populate with member rules.

      my-rule-set

    4. Click Add Rule to create a new rule.

      add-new-rule

      CodeRush adds the new rule to the rule set.

      default-rule-options

    5. Change the suggested rule name to “Private fields” and press Enter to save this change.

      change-rule-to-private-fields

    6. In the “Group by” section, specify the following condition for the “Private fields” rule:

      grouping-for-private-fields-rule

      For more information on how to configure grouping, see the following example: How to: Create a Rule

    7. Add a rule (see the step 4), name it “Private events”, and specify the following grouping condition:

      private-events-rule

    8. Add a rule for private methods with the following grouping condition:

      private-methods-rule

    9. Select the “Private fields” rule and click Region to wrap this rule into a region.

      wrap-private-fields-rule

    10. Press Enter to apply the suggested region name for private fields.

      region-text-field

      CodeRush shows the “Member section name” field set to “Fields” in the right part of the options page.

      Note

      CodeRush can wrap fields, methods, or properties into a region when you expand a template. For more information, see the following topic: Member Sections.

    11. Repeat steps 9-10 to wrap “Private events” and “Private methods” rules into a region.

      wrap-private-fields-events-methods

    12. Select the “Region: Private methods” item and click Region.

      region-for-region-private-methods

    13. Change the suggested region name to: “Private fields, private events, and private methods”.

      private-fields-events-methods-region

    14. Use drag & drop to position “Region: Private events” and “Region: private fields” items inside “Region: Private fields, private events, and private methods”, as shown in the screencast below:

      drag-drop-regions-inside-parent

    15. Click Apply and OK to save the changes, and close the Organize Members options page.

    16. Place the caret in a class member in the code snippet below:

      using System;
      
      namespace MyApp
      {
          public class Example
          {       
              object field1;
              event EventHandler EventName1;
              void MethodName1() { }
              object field2;        
              event EventHandler EventName2;      
              void MethodName2() { }        
          }
      }
      
    17. Press Ctrl+. or Ctrl+~ to invoke the Code Actions menu.

    18. Choose Organize Members from the menu and press Enter.

      wrap-section

    CodeRush groups private fields, private events, and private methods according to the specified rule order and wraps these members into regions.