RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Code Refactoring in VS.NET 2005 : Page 3

Find out how to use VS.NET 2005's Refactoring engine to improve the structure and readability of your C# source code while preserving its functionality.

Promote Local Variable to Parameter
This option lets you promote a local variable in a method to a parameter. Note that the variable should be initialized for this option to work. After the promotion, VS updates all calls to this method reflect this change.

Consider the following method:

   public int Add(int i,int j)
      int k = 0;
      k = i + j;
      return k;
For example, suppose you promote the local variable k in the preceding code to a method parameter using the "Promote..." option. Here's the result. Note that the method declaration changes, adding the new parameter k:

   public int Add(int i, int j, int k)
      k = i + j;
      return k;
Remove Parameters
You use this option to remove one or more parameters from a method and update all the calls to the method accordingly. Figure 6 shows the Remove Parameters dialog.

Figure 6. The Remove Parameters Dialog: Select the parameter(s) you want to remove, and then click OK.
Figure 7. Remove Parameter Preview: The preview lets you see the code result of removing parameters before VS applies the changes.
Select the parameter(s) that you would like to remove and then click on the OK button. A preview window (see Figure 7) pops up showing you the outcome. At this point you can still cancel the operation if you don't like the result.

Reorder Parameters
A simpler, but welcome operation lets you reorder the parameters in a method. This also updates any calls to this method accordingly. The dialog lists the parameters. You can move parameters around in the list by selecting them, and then clicking an arrow key (see Figure 8).

Figure 8. Reorder Parameters: In this dialog, you move the parameters in the list until they're in the preferred order, and then click OK.
Figure 9. Reorder Parameters Preview: After switching the order of the two parameters "i" and "j," the preview shows you the result in code. You can still cancel the operation at this point.
Change the order of the parameters as per your requirements and click on the OK button. You'll see the preview window in Figure 9.

As you've seen, Visual Studio's code refactoring features let you improve the design of your existing code, facilitating its readability and simplifying its structure, all without changing its functionality.

Joydip Kanjilal has over 10 years of industry experience with C, C++, Java, C#, VB, VC++, ASP.Net, XML, Design Patterns, UML, etc. He currently works as a senior project leader in a reputable multinational company in Hyderabad, India, and has contributed articles on .NET and related technologies to www.aspalliance.com.
Email AuthorEmail Author
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date