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:
Open the Editor | All Languages | Organize Members options page to configure the Organize Members feature.
In the “Rule set” section, click New to open the “New Set” dialog.
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.
CodeRush creates an empty rule set that you can populate with member rules.
Click Add Rule to create a new rule.
CodeRush adds the new rule to the rule set.
Change the suggested rule name to “Private fields” and press Enter to save this change.
In the “Group by” section, specify the following condition for the “Private fields” rule:
For more information on how to configure grouping, see the following example: How to: Create a Rule
Add a rule (see the step 4), name it “Private events”, and specify the following grouping condition:
Add a rule for private methods with the following grouping condition:
Select the “Private fields” rule and click Region to wrap this rule into a region.
Press Enter to apply the suggested region name for private fields.
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.
Repeat steps 9-10 to wrap “Private events” and “Private methods” rules into a region.
Select the “Region: Private methods” item and click Region.
Change the suggested region name to: “Private fields, private events, and private methods”.
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:
Click Apply and OK to save the changes, and close the Organize Members options page.
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() { } } }
Press Ctrl+. or Ctrl+~ to invoke the Code Actions menu.
Choose Organize Members from the menu and press Enter.
CodeRush groups private fields, private events, and private methods according to the specified rule order and wraps these members into regions.