question about one simple refactoring

Hi, I got one simple question about typical refactoring pattern - is there a shortcut in ReSharper for this operation?

I got this:

try

{

    DirectoryInfo di = new DirectoryInfo(Path.Combine(basePath, folder));

}

catch (ArgumentException e)

{

    // do something

}

And I want this :

DirectoryInfo di;

try

{

    di = new DirectoryInfo(Path.Combine(basePath, folder));

}

catch (ArgumentException e)

{

    // do something

}

di.doSomething

Just moving inner variable outside current context

2 comments

You can do it in two stages:

Alt-Enter on the 'di' and choose 'split assignment and declaration'
Alt-Enter on the first 'di' and choose 'move to outer scope', OR do CTRL-SHIFT-ALT-LEFT on the first 'di'.

I don't know a way to do it in one.

0

Hello,

Well, if you write a usage of "di" outside the scope and it gets red because
it's not visible there, ReSharper has a quickfix called "Use existing variable"
which does this in one step.

Unfortunately, in this case you cannot use code completion when you write
"di" outside the scope, which could be a problem with nontrivial variable
names. That's why I do not consider it a good one-step solution.

But if you add the "try-catch" construct to some existing code, e.g. with
"Surround With | try {} catch {}" patten, then this quick fix is just what
you need.


Serge Baltic
JetBrains, Inc — http://www.jetbrains.com
“Develop with pleasure!”


0

Please sign in to leave a comment.