views:

25

answers:

1

I am using Resharper 4.5 in Visual Studio 2008. Whenever I try to extract a block of code into a method, it tries to create a subroutine and not a function. The return type option is disabled. Does anyone have any advice as to how I can get it to create a function and not a subroutine?

thanks!

+1  A: 

It's likely that the code you've highlighted doesn't have anything to return. If the code you've highlighted doesn't set variables that are used further down your code then there's nothing for your refactored code to return.

For example, if I highlight this code and Extract Method...

        Program p = new Program();
        p.DoStuff();

... there's nothing to return (I don't reference p beyond this code). If I highlight the first 2 lines from this code...

        Program p = new Program();
        p.DoStuff();
        p.DoMoreStuff();

... then Resharper will create a method returning an instance of Program (i.e. "p").

Martin Peck
It seems that is depends on how you declare the variable. In your example above, if I do (in VB): "Dim p As New Program" and highlight what you say, it still does a subroutine, BUT if I do: "Dim p as Program = New Program", then it returns a function. I think that is stupid, but thanks!
Dan Appleyard