1

I have a lot of code like this:

ml = this.CriteriaList.ItemByName(SC_STATIONS) as MultiList;

I'd like to replace it with

ml = (MultiList) this.CriteriaList.ItemByName(SC_STATIONS);

To that end, I went into Resharper Options, selected Custom Patterns and created a new one that has the following as the Search Pattern:

`$expr1$ as $type1$;`

with expr1 meaning the expression and type1 meaning type. The replace pattern is ($type1$) $expr1$;.

The problem is that Resharper never finds anything that matches this custom pattern.
What am I missing here?

AngryHacker
  • 59,598
  • 102
  • 325
  • 594
  • does it **have** to be on ReSharper? – Andre Calil May 10 '13 at 17:32
  • @AndreCalil It does not. – AngryHacker May 10 '13 at 17:39
  • Why do you want to do that? You are aware that the different syntaxes actually mean different things? – Lasse V. Karlsen May 10 '13 at 19:35
  • @AngryHacker This is a duplicate or rather a continuation of http://stackoverflow.com/q/16471489/275751 which you asked earlier isn't it? – Piers Myers May 10 '13 at 19:40
  • @LasseV.Karlsen: personally, I see code where the authors used the `as` syntax _instead of_ the `(type)` syntax without making use of the fact that `as` returns null if the cast would have failed. This means that when the cast _does_ fail, you just get a `NullReferenceException` some time later. – John Saunders May 10 '13 at 19:51
  • @PiersMyers Yes, I am trying to get it to the point where it works on all variations of the code base. – AngryHacker May 10 '13 at 21:44
  • My point was that ReSharper should help tell you which situation you're in. If you use `as`, and then dereference it, ReSharper should warn you about a potential null reference exception. Likewise, if you use cast after `is`, ReSharper should tell you that you should use `as` instead. My point was that a global search-and-replace to remove `as` with a hard cast seems to focus on the wrong thing, that `as` is somehow unwanted. Perhaps for that particular code base it might be, but to be honest, I doubt it. – Lasse V. Karlsen May 11 '13 at 12:57

2 Answers2

1

This will work, although it's not on ReSharper.

Open Visual Studio Replace window (CTRL + H), turn on the Use regular expression option and use the following terms:

  • Find: (.*)(\s|\((?!\))|\=)(.+) as (\w+)(\).*|;)
  • Replace: $1$2($4)$3$5

Note: if you're using VS2010, you should use {} instead of () and \ instead of $. Go figure...

Andre Calil
  • 7,652
  • 34
  • 41
  • I am afraid, it doesn't really work, but I see the potential. The following line: `Selected_AE = (this.CriteriaList.ItemByName(ReportCommon.CRITERIA_AES_VIERO) as MultiList).SelectedValues.Count;` gets converted into `Selected_AE = (MultiList).SelectedValues.Count) (this.CriteriaList.ItemByName(ReportCommon.CRITERIA_AES_VIERO);` – AngryHacker May 10 '13 at 18:08
  • You will also find that it will match any strings or comments that contain 'as' and swap the words before and after it. – Piers Myers May 10 '13 at 18:14
  • @PiersMyers Indeed, as long as the comment has the syntax `(anything) = (anything) as (anything);`, which is somewhat an awkward comment – Andre Calil May 10 '13 at 18:30
  • @JohnSaunders Read my comment under the question – Andre Calil May 10 '13 at 19:25
  • @AngryHacker Edited, could you try again? – Andre Calil May 10 '13 at 19:48
1

This Resharper Custom Pattern seems to do what you want:

enter image description here

The small program I used to test this pattern was:

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var b = FooFactory() as bar;
        }

        static private foo FooFactory()
        {
            return new foo();
        }
    }

    class foo {}

    class bar : foo {}
}

and it should put a wiggly line under FooFactory() as bar offering you the option to replace it with (bar)FooFactory()

Adding var ml = CriteriaList.ItemByName(SC_STATIONS) as MultiList; and doing a 'Search Now' on this custom pattern gives me this:

enter image description here

Piers Myers
  • 10,611
  • 6
  • 46
  • 61
  • That is what I did. Click save and then press Search button on the Custom Patterns dialog and see if anything is found. My couldn't find anything. – AngryHacker May 10 '13 at 18:34
  • @AngryHacker how odd, have you tried doing it as a 'Search with Pattern'? Are you sure you have c# selected? My test was done in VS2010 and Resharper 7.1.2 – Piers Myers May 10 '13 at 18:39
  • On my box it also finds simple expressions such as `var b = FooFactory() as bar`. Try my example: `ml = this.CriteriaList.ItemByName(SC_STATIONS) as MultiList;` – AngryHacker May 10 '13 at 18:53
  • that works for me too even without defining `CriteriaList`, `SC_STATIONS` or `MultiList`. – Piers Myers May 10 '13 at 19:05