views:

65

answers:

2

What is the easiest way of pulling an excisting method out of it's class and into a new class using Visual studio 2010 / Resharper?

Edit: I use Resharper version 5.

+2  A: 

Starting with

public void Method() {}

  1. First, make the method static using the "Make Method Static" command.

    public static void Method(){}

  2. Then, add a local variable of the type of the new class:

    public static void Method(){Class2 me = new Class2();}

  3. Then, use Introduce Parameter

    public static void Method(Class2 me) {}

  4. Then use "Make Method non-Static". In class2:

    public void Method(){}

John Saunders
I fail to see the <move method> step, is it implicit?I also fail to see the gain of the variable => parameter step.
Nasit
The method moves when you (1) add the parameter and then (2) make the method non-static, which turns the parameter into the "this" pointer, which makes the method an instance method of that class.
John Saunders
After trying both methods some I reallocted the check to this answer.
Nasit
I switch place one step 1 and 2. Find it less cumbersome when Class2 takes arguments in the constructor that I have as fields in Class1.
Nasit
+2  A: 

Same as above, but I would not do the conversion to static-method manually. Pull up the "Refactor this" menu (using shortcuts of course, ctrl+shift+R), then select "Make method static", then "Refactor this"->"Move".

Note:

If you're talking about moving a method in a class hierarchy, you can use "Push members down" or "Pull members up"

Marius
I didn't mean you should make the method static manually. I meant "Make Method Static". Also, doing a "Move" next will leave the method static, and it wasn't static before.
John Saunders