Creating New Overloaded Methods in Visual Studio C# is Really Easy with CodeRush

Posted by Peter Kellner on July 26, 2010 · 4 mins read

So, you want to add a couple extra parameters to an existing method in Visual Studio, while not having to change all your existing code to call the new method signature?  It’s easy with CodeRush from Devexpress.  In this post, I’ll start with a simple method and add some parameters to it, then do the magic refactor.

Here is the starting code.

public static int SynchronizeScopesAsyncStart(
int dbSyncPairId, string schemaName,
string connectionStringLeft,
string scopeLeft, string connectionStringRight,
string scopeRight, bool skipDbLogging)
{...

So, We need to add a couple new parameters as follows.

public static int SynchronizeScopesAsyncStart
(int dbSyncPairId, string schemaName,
string connectionStringLeft,
string scopeLeft, string connectionStringRight,
string scopeRight, bool skipDbLogging,
int pauseMsScopeLeft,int pauseMsScopeRight)
{

So now, we want to create an overloaded method with the two new parameters (pauseMsScopeLeft and pauseMsScopeRight) left out. 

The way we do this is first, hover the cursor over the method name (SynchronizeScopeAsynStart) as follows.

image

Notice the helper message telling us

Create an overloaded method similar to this one with fewer parameters.

This is exactly what we want to do so we chose this by pressing enter.  CodeRush now gives us a red arrow pointer telling us where it will put the overloaded method by default as well as giving us some instructions on how to put the method someplace else.  In our case, I’m happy to have it right below where the original method is, so I just press enter.

image

Now, is the really clever part.  I can simply follow the little helper window and move through the parameter list and exclude parameters I don’t want in the parameter list of my new overloaded method.  In my case, I want to get rid of the last two parameters.  It highlights the last one to start, so I simply press “space” as it says, then I press Shift-Tab to get to the previous parameter, space bar again to exclude that one and I’m almost done.  Here is the screen I’m now looking at.

image

Finally, I press enter to commit my changes and I have a new overloaded method that looks like the following.

image

You can continue with the refactoring and tell CodeRush to “inline” your temporary variables, but I think this is enough for now.

Hope this helps!