Resharper allows to replace only one issue. How to replace all same issues in entire proect?
For example, to replace
ctx.Shops.Where(t => t.ShopId == currentBase.ID).Single()
by
ctx.Shops.Single(t => t.ShopId == currentBase.ID)
you should push button as shown in the
How to replace all accurances of Sinle method in entire project?
Version of resharper 9.1 and 10
Hmm, fixing this for entire file/project/solution (aka "fix in scope") does not seem to be supported.
The closest I could think of is selecting Inspection "Replace with single call..." > Find similar issues..., and then navigate between the results and fix manually.
Related
So this is going to be a kind of strange question, I have never run into this formatting issue with any other programs I have written. I think it is because my if statements has multiple || which needed multiple lines to make it appear more readable.
Notice the dotted line under my else if(). It is getting shifted right for some reason under the if instead of the else.
Is there some kind of formatting setting that can fix this? Or is there a different way I am supposed to do this else if statement? Having all those OR checks is pretty much necessary but I did not wan't to put them all on a single line as that would look real ugly.
Thanks!
Tools -> Options -> Text Editor -> C# -> Code Style -> Formatting -> New Lines
Uncheck everything and save.
Then, CTRL + E, D to format the document.
Also, you're using a huge compound if statement there, which is quite frankly a mess. If you need to check multiple items as starting values, put them all into a list.
List<String> ModelNumberPrefixes = new List<String>();
ModelNumberPrefixes.Add("A1C1C");
ModelNumberPrefixes.Add("A1C1D");
//etc
ModelNumberPrefixes.ForEach(s => {
if (ModelNumber.StartsWith(s)) {
//Whatever you need to do in your big if block
}
});
To format a selection: Ctrl+K, Ctrl+F
To format a document: Ctrl+K, Ctrl+D
See the pre-defined keyboard shortcuts . (These two are Edit.FormatSelection and Edit.FormatDocument.)
I have lots of code like below:
PlusEnvironment.EnumToBool(Row["block_friends"].ToString())
I need to convert them to something like this.
Row["block_friends"].ToString() == "1"
The value that gets passed to EnumToBool is always unique, meaning there is no guarantee that itll be passed by a row, it could be passed by a variable, or even a method that returns a string.
I've tried doing this with regex, but its sort of sketchy and doesn't work 100%.
PlusEnvironment\.EnumToBool\((.*)\)
I need to do this in Visual Studio's find and replace. I'm using VS 17.
If you had a few places where PlusEnvironment.EnumToBool() was called, I would have done the same thing that #IanMercer suggested: just replace PlusEnvironment.EnumToBool( with empty string and the fix all the syntax errors.
#IanMercer has also given you a link to super cool, advanced regex usage that will help you.
But if you are skeptical about using such a complex regex on hundreds of files, here is what I would have done:
Define my own PlusEnvironment class with EnumToBool functionality in my own namespace. And then just replace the using Plus; line with using <my own namespace>; in those hundreds of files. That way my changes will be limited to only the using... line, 1 line per file, and it will be simple find and replace, no regex needed.
(Note: I'm assuming that you don't want to use PlusEnvironment, or the complete library and hence you want to do this type of replacement.)
in Find and Replace Window:
Find:
PlusEnvironment\.EnumToBool\((.*))
Replace:
$1 == "1"
Make sure "Use Regular Expressions" is selected
The project I'm working on (C# on VS 2015 with ReSharper 2016.1.2) has a new requirement which requires us to remove all usages of p_ prefixes in parameter names (p_Param becomes param).
I'd like to create a ReSharper Code Inspection Custom Pattern to take care of this for me (to match the string pattern with squiggly lines and auto-fix in the solution).
I've followed the tutorial at https://www.jetbrains.com/help/resharper/2016.1/Code_Inspection__Creating_Custom_Inspections_and_QuickFixes.html but I'm a bit stuck.
I've tried the following patterns:
Var 1
Find: $prefix$$varName$ ($prefix$ - identifier, matching regex [^p_*] (start with p_, continue with anything, $varName$ - identifier)
Replace $varName$
Var 2
Find: p_$varName$ ($varName$ - identifier)
Replace $varName
I'd also need to transform the $varName$ identifier from PascalCase to cammelCase (no ideea how to do this).
When searching via 'Search now' - no results are found in either situation.
Any help is appreciated.
Using ReSharper, you can change the naming style of variables and have it apply to an entire solution.
The location of this option will (probably) vary but for my version of R# (2016.1.2), it's under ReSharper->Options->Code Editing->C#->Naming Style:
From there, change the Entity Kinds to how you want them to appear. In mine, I prefer _lowerCamelCase for private instance fields for example.
Once your changes are made, find any field of that type in code (I'll use a private variable) that doesn't follow that format, click it and then click the light bulb to the left. From there mouse-over the arrow on "Rename to ......" and select Fix naming in solution.
You might have to do it a few times but that's how I rename stuff based on my preferred code style.
So, in my ASP.NET C# code base I have possibly hundreds of bits of code like this:
Response.Redirect("something.aspx?Error=" + ex.Message);
I want to automatically add an argument to all of these method calls to add 'true' as the second parameter to this method, like this:
Response.Redirect("sometihng.aspx?Error=" + ex.Message, true);
I have Visual Studio 2010 and the latest version of Resharper at my disposal.
I tried using the 'Search with Pattern' feature in Resharper (VS menu -> ReSharper -> Find -> Search with Pattern) to see if this would automatically refactor my codebase, but I'm not sure exactly how or if it works. Here's what I tried:
On the right-hand side, I created an 'Argument' placeholder called 'anyString', in the hope that this would find and replace all invocations of Response.Redirect that have a string in the first argument, but this found no matches in my code-base.
Any ideas on how I might solve this without resorting to manually changing all references?
As per the Jetbrains Resharper documentation on 'Searching a Code with Pattern':
Pay attention, that when you use a placeholder, its name should be
enclosed with dollar signs (use the syntax $xx$, where xx represents
placeholder name),whereas when you create a new placeholder, you
should omit these special symbols.
Therefore, I was on the right track. Also for the placeholder I just need '$anyString$' and it will find all invocations of the method, even if they are made up multiple string objects (e.g. string literals and string objects). So this is how it would look:
The 'anyString' placeholder pattern was created by performing the following steps:
1) Click 'Add Placeholder' -> Argument
2) Give it a name, e.g. 'anyString'
For my case, I also checked the 'Limit minimal number of arguments' and selected 1, and I also checked the 'Maximal' box and set that to 1 also.
The 'Save' button is also useful if you intend on reusing the pattern again.
I made the mistake of using == for comparing IP addresses instead of using the equals() method of the IPAddress class in C#, which will result in the comparison of references instead of values.
Since the solution I am currently working on is very large for a one-man project (> 100.000 lines of source code), I am very sure that I still have some of these wrong statements in my code.
Is there any possibility to tell Visual Studio to find all occurrences of == operations on a specific class for me, so that I can find and clean up the bugged comparisons?
with best regards, emi
It's a bit of a hack but you can temporarily add this class to your project:
namespace System.Net
{
class IPAddress
{
[Obsolete]
public static bool operator ==(IPAddress a, IPAddress b) { return true; }
[Obsolete]
public static bool operator !=(IPAddress a, IPAddress b) { return true; }
}
}
Compile and look for warnings about using obsolete methods:
Warning 'IPAddress.operator ==(IPAddress, IPAddress)' is obsolete
Once you have fixed the code, remove the class definition.
You could always use a find / replace on "==". You can use the filters to determine what / where you want to search or just use the Entire Solution.
You might be able to use .NET Reflector or maybe the Visual Studio call hierarchy window to look for calls to the operator== method of the IPAdress class. I don't know if this is possible, just throwing out an idea.
If you know the name of the variable representing the IP address over your code, then yes, it is possible with some workaround. Say your variable is called 'ipAddress'. Then do this:
Using wildcards search for:
ipAddress*==
Then loop through the results and make a macro that make the change for you. For example, let's suppose your statement looks like this:
if (ipAddress == anotherIpAddress) {
Then you make a micro as follows:
Start Recording
Press Home # This will go to the beginning of the line
Ctrl+Right Three Times # This will keep the cursor on the beginning of anotherIpAddress
Backspace # This will remove the space
.equals( # This will write .equals(
Del Three Times # This will delete the == and the space after it
Ctrl+Right # This will keep you at the closing bracket ).
) # This will write another closing bracket for the equals functions.
Stop Recording
Now you have a macro that change the line for you. All you have to do is to repeatedly press F4 then Ctrl^P. Pressing F4 moves you to the next results in Find in Files (I suppose you will use this), and pressing Ctrl^P executes the macro.
There is a better solution actually using regular expressions but I am not sure whether it works with visual studio. Basically, it groups elements in Find and use them in Replace. So you search for something like "ipAddress == ( < my variable pattern > )" and replace it with "ipAddress.equals(\1)", the one here refers to the first group.
Hope that helps!
You might subclass IPAddress and override the == operator. This of course depends on how easily you can replace the references. Once you've done that, you could stop there or replace all instances of your == operator with .Equals()