tags:

views:

85

answers:

3

My C# code has a lot of statement like "this.Name=...". In order to make my code neat, I let the text editor to replace all "this." to nothing. The code still worked. But later I fund it caused me a lot of new troubles for I wrote some statements like:

this.Name = Name; // the second Name is a parameter.

After the replacement, it became:

Name = Name;

Now, I met too much code. How to find the suspicious code like "Name = Name;" by Regex in VS 2010?

Thanks,

Ying

+6  A: 

Why would you want to use Regex when you can simply compile the solution and look for the CS1717 warning:

Assignment made to same variable; did you mean to assign something else?

Also note that in C# it is a good convention to have your parameters start with lowercase letter.

Darin Dimitrov
The compiler found my casual mistakes. Thank you.Ying
Ying
+5  A: 

I would agree that Darin's approach is more robust and should be done first. However you might have commented out sections of code which will be missed with this approach.

To try and find those you can use "Find in Files". In the Find box tick "Use regular expresssions" and enter {:i}:Wh*=:Wh*\1

  • :i C Style identifier ("tagged" expression by enclosing in braces)
  • :Wh* Zero or more white space chars
  • \1 back reference to tagged identifier found

This approach might bring back some false positives so you could try :Wh+{:i}:Wh*=:Wh*\1:Wh+ if there are too many but at the risk of missing some matches (e.g. where the closing comment mark is immediately after the assignment statement)

Martin Smith
Wouldn't you need to capture that, or does the `{:i}` magic do that for you? (If it's the curly parens that are doing it, it's a *very* different RE dialect to the ones I've used…)
Donal Fellows
change `:Wh+` to `:Wh*` since there might not be white spaces...
FallingBullets
@Falling - Good point. Done. @Donal - Yes. The Visual Studio IDE uses some odd Regex flavour of its own!
Martin Smith
+1  A: 

You could restore your last commit from your CVS, if you haven't changed too much since.

The problem with doing what you ask is that there might be other cases where "this" shouldn't have been replaced and you haven't seen the problem yet.

Sylverdrag
+1, excellent suggestion about restoring from source control.
Darin Dimitrov