Re: Extract method refactor (C# in VS 2010)

Chris Anderson <[email protected]> Wed, 19 May 2010 10:31:39 -0400
Newsgroups gmane.comp.windows.devel.dotnet.advanced
Message-ID <[email protected]>
In your first section of code, if you find a "b" you return out of DoSome=
thing, and x is never set to "Hello"

In your hand-refactored code, if you find a "b", you return out of f - an=
d execution continues, x is set to "Hello", etc
(i.e. your hand-refactored code is not equivalent)

Chris


On Wed, 19 May 2010 10:20:39 -0400, David Nicholson <[email protected]=
K> wrote:

>I have a function like this:
>
>        private void DoSomething(List<string> slist)
>        {
>            foreach (string s in slist)
>            {
>                if (s =3D=3D "b")
>                    return;
>            }
>
>            string x =3D "Hello";
>        }
>
>and I select all of the foreach loop. If I refactor/extract method I get=
 a message saying that all paths must be terminated by a return statement=
 if any are.
>
>If I remove the assignment to x at the end (outside the part I want to r=
efactor), it works.
>
>I don't understand why the assignment stops it doing something like this=
:
>
>        private void DoSomething(List<string> slist)
>        {
>            f(slist);
>
>            string x =3D "World";
>        }
>
>        private static void f(List<string> slist)
>        {
>            foreach (string s in slist)
>            {
>                if (s =3D=3D "b")
>                    return;
>            }
>        }
>
>Thanks
>David.

=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
View archives and manage your subscription(s) at http://peach.ease.lsoft.com/archives