Hack 9. Master Regions![]() Studio's regions let you make even the largest source files become instantly readable. Regions are a valuable part of organizing your code and making it more readable. Using regions, you can select a section of code that you want to be able to logically group and also collapse. In this hack, you will learn how to use regions in both C# and VB.NET and learn about an add-in that makes working with regions even easier. 2.5.1. C# RegionsTo use regions in C#, you simply need to surround the code you want to include in the region with #region and #endregion. Optionally, you can also include a string literal after #region that will be shown when the region is closed. Figure 2-15 shows an example of C# code surrounded by a region. Figure 2-15. C# region expanded![]() Figure 2-16. C# region collapsed![]() even inside of methods or properties. (Whether this is a good idea is debatable.) Regions can also be nested inside of other regions. 2.5.2. VB.NET RegionsVB.NET regions are a little bit different than C# regions. In VB.NET, you surround the code that you want to include in the region with #Region and #End Region. You can also include a string after the #Region, but in VB.NET, the string will need to be enclosed in quotes. Figure 2-17 shows an example of VB.NET code surrounded by a region. Figure 2-17. VB.NET region expanded![]() complete left of the document. In VB.NET, you can include regions inside of other regions, but you can't include them inside of methods or properties. 2.5.3. Region StrategyRegions are best used to group similar parts of your class. This makes finding a particular method or property easier and also makes the file more readable, since sections that you are not working with are hidden inside of a region. I am a big fan of the idea that all code, except for namespace declarations and using statements, should be in a region. Opening a file in which all of the private member variables are grouped inside of a region, the constructors in another region, the private methods in another region, and the public properties in yet another region make it extremely easy to find exactly what you are looking for. There are times when this organization might not make the most sense though. When implementing an interface, it is a good practice to group the method and properties that are used to implement the interface into a single region.The best thing to do with regions is to define a standard for your project and then be sure that all of your developers follow that standard. A well-defined region strategy can make it easier to find and understand files, whereas a poorly defined strategy can lead to regions being more of a hindrance than benefit. 2.5.4. The Regions Add-inTo enclose a piece of existing code in a region, you need to find the top of the code, type in the #region statement, and then find the bottom of the code and type in the closing #endregion statement. While this is not all that difficult, wouldn't it be nice to be able to simply highlight a section of code and click a button that adds that code to a region? That is where the Regions Add-in comes into play. Using the Regions Add-in, you can select a piece of code using the mouse or keyboard and then right-click and select Add to New Region.First, you will need to download and install the Region Add-in from http://www.codeproject.com/dotnet/RegionsAddIn.asp. After you have downloaded and installed the add-in, you will see two new items on your right-click menu. These are shown in Figure 2-18. (These items will be visible only when you have text selected.) Figure 2-18. Regions Add-in menu items![]() item, you will see the dialog shown in Figure 2-19. Figure 2-19. New Region Name dialog![]() will wrap the selected code in a region using the name you entered.You can also use the add-in to add code into an existing region. Simply select a section of code and then right-click and choose the Add to Existing Region menu item. You will then see the Regions Dialog shown in Figure 2-20. Figure 2-20. Regions Dialog![]() the selected code to or you can decide to add it to a new region and click the Add New Region checkbox. This is particularly useful if you are adding a new property to the class; instead of opening the existing region and appending it to the end, you can write the property anywhere you want and then select it and move it to that region.The Regions Add-in is a valuable tool that makes working with regions even easier than it already is. |