Visual Studio - Tips & Tricks: Remove Parameters and Reorder Parameters in Visual Studio
Introduction:
In this article i will explain two features provided in Visual studio IDE. Namely they are "Remove Parameters" and "Reorder Parameters".
Remove Parameters:
Assume we have a method with 10 parameters. And we are using that method in many places. Now my requirement is to remove 3rd parameter from that method. How we will do? First we will remove 3rd parameter from method, then we will remove 3rd parameter wherever the method is called. Normally people will follow either of the following ways:
- Find all references of the method and remove the 3rd parameter manually
- Remove 3rd parameter from the method and build the application. Now fix the errors wherever it is shows
But we have a very usefull feature called Remove Parameters in Visual Studio IDE. Which will helps developers to remove paramater from the method as well as its references.
Assume we have a class Item as shown below:
public class Item
{
public string ItemCode { get; set; }
public string ItemDesciption { get; set; }
public string UOM { get; set; }
public int ProductID { get; set; }
public bool IsActive { get; set; }
public void AddItems()
{
List<Item> lstItems = new List<Item> {
new Item {
ItemCode = "A0001",
ItemDesciption = "1 LITER BOTTLE",
ProductID = 1,
UOM = "NUM",
IsActive = true
},
new Item {
ItemCode = "A0002",
ItemDesciption = "1.5 LITER BOTTLE",
ProductID = 1,
UOM = "NUM",
IsActive = true
},
new Item {
ItemCode = "A0003",
ItemDesciption = "2 LITER BOTTLE",
ProductID = 1,
UOM = "NUM",
IsActive = true
}
};
foreach (Item item in lstItems)
{
AddItem(item.ItemCode, item.ItemDesciption, item.UOM, item.ProductID, item.IsActive);
}
}
public void AddItem(string itemCode, string itemDescription, string uom, int productID, bool isActive)
{
// Add Item to Database logic goes here.
}
}
Now my requirement is to remove isActive parameter from AddItem(...) method as well as it's references. Now what we will do is goto AddItem(...) method right click (R + Click) on it. It will shows some context menu. There we will find Refactor >> Remove Parameter as shown below.
Select Remove Parameters option, then it will shows all the parameters in a popup window which are present in that AddItem(...) method.
Now select which parameter you want to delete, click on Remove button. Removed parameters will be striked first, When OK button is clicked then it will be removed.
Now click OK button, then it shows Preview Changes window to make code changes as shown below:
Now click Apply button, parameter will be removed.
Note: Suppose our method is referenced in 4 places then all the four methods will be dispalyed in Preview Changes window. By default all the methods are checked for changes. But i don't want to remove parameter from particular calling method. In this case we will uncheck that method so that changes will not be reflected to that method.
Output:
public class Item
{
public string ItemCode { get; set; }
public string ItemDesciption { get; set; }
public string UOM { get; set; }
public int ProductID { get; set; }
public bool IsActive { get; set; }
public void AddItems()
{
List<Item> lstItems = new List<Item> {
new Item {
ItemCode = "A0001",
ItemDesciption = "1 LITER BOTTLE",
ProductID = 1,
UOM = "NUM",
IsActive = true
},
new Item {
ItemCode = "A0002",
ItemDesciption = "1.5 LITER BOTTLE",
ProductID = 1,
UOM = "NUM",
IsActive = true
},
new Item {
ItemCode = "A0003",
ItemDesciption = "2 LITER BOTTLE",
ProductID = 1,
UOM = "NUM",
IsActive = true
}
};
foreach (Item item in lstItems)
{
AddItem(item.ItemCode, item.ItemDesciption, item.UOM, item.ProductID);
}
}
public void AddItem(string itemCode, string itemDescription, string uom, int productID)
{
// Add Item to Database logic goes here.
}
}
Reorder Parameters:
This is similar to Remove Parameters. But in Reorder Parameters instead of removing parameters we will change the order/sequence of the parameters. Goto AddItem(...) method right click (R + Click) on it. It will shows some context menu. There we will find Refactor >> Reorder Parameter as shown below.
Select Reorder Parameters option, then it will shows all the parameters in a popup window which are present in that AddItem(...) method. Now i'm moving productID parameter to first position. Select prodcuID, select up arrow button so that it will be moved towards up.
Once it is moved OK button will be enabled. At the same time you can see the Preview method signature in Reorder Parameters window.
Now click OK button, parameters order/sequence will be updated in all the places.
Output:
public class Item
{
public string ItemCode { get; set; }
public string ItemDesciption { get; set; }
public string UOM { get; set; }
public int ProductID { get; set; }
public bool IsActive { get; set; }
public void AddItems()
{
List<Item> lstItems = new List<Item> {
new Item {
ItemCode = "A0001",
ItemDesciption = "1 LITER BOTTLE",
ProductID = 1,
UOM = "NUM",
IsActive = true
},
new Item {
ItemCode = "A0002",
ItemDesciption = "1.5 LITER BOTTLE",
ProductID = 1,
UOM = "NUM",
IsActive = true
},
new Item {
ItemCode = "A0003",
ItemDesciption = "2 LITER BOTTLE",
ProductID = 1,
UOM = "NUM",
IsActive = true
}
};
foreach (Item item in lstItems)
{
AddItem(item.ProductID, item.ItemCode, item.ItemDesciption, item.UOM, item.IsActive);
}
}
public void AddItem(int productID, string itemCode, string itemDescription, string uom, bool isActive)
{
// Add Item to Database logic goes here.
}
}
That's it. I hope you enjoyed this article. We will add more articles on "Tips & Tricks" in the coming days.
-
CreatedApr 07, 2015
-
UpdatedOct 03, 2020
-
Views2,394