Code refactoring with Visual Studio 2010-Part 3
- by Jalpesh P. Vadgama
I have been writing few post about Code refactoring features of visual studio 2010 and This blog post is also one of them. In this post I am going to show you reorder parameters features in visual studio 2010. As a developer you might need to reorder parameter of a method or procedure in code for better readability of the the code and if you do this task manually then it is tedious job to do. But Visual Studio Reorder Parameter code refactoring feature can do this stuff within a minute. So let’s see how its works. For this I have created a simple console application which I have used earlier posts . Following is a code for that. using System;
namespace CodeRefractoring
{
class Program
{
static void Main(string[] args)
{
string firstName = "Jalpesh";
string lastName = "Vadgama";
PrintMyName(firstName, lastName);
}
private static void PrintMyName(string firstName, string lastName)
{
Console.WriteLine(string.Format("FirstName:{0}", firstName));
Console.WriteLine(string.Format("LastName:{0}", lastName));
Console.ReadLine();
}
}
}
Above code is very simple. It just print a firstname and lastname via PrintMyName method. Now I want to reorder the firstname and lastname parameter of PrintMyName. So for that first I have to select method and then click Refactor Menu-> Reorder parameters like following.
Once you click a dialog box appears like following where it will give options to move parameter with arrow navigation like following.
Now I am moving lastname parameter as first parameter like following.
Once you click OK it will show a preview option where I can see the effects of changes like following.
Once I clicked Apply my code will be changed like following.
using System;
namespace CodeRefractoring
{
class Program
{
static void Main(string[] args)
{
string firstName = "Jalpesh";
string lastName = "Vadgama";
PrintMyName(lastName, firstName);
}
private static void PrintMyName(string lastName, string firstName)
{
Console.WriteLine(string.Format("FirstName:{0}", firstName));
Console.WriteLine(string.Format("LastName:{0}", lastName));
Console.ReadLine();
}
}
}
As you can see its very easy to use this feature. Hoped you liked it.. Stay tuned for more.. Till that happy programming.