My visual studio doesn't build the solution if any of the following errors occurs:
1. Invalid spacing around ';'
2. All private methods must be declared after protected.
3. Constructor must be placed before method declarations.
4. Must use curly brackets in if condition.
5. Use string.empty instead of "".
Now I know we should follow correct coding practice, but I'm working on an old code that has these kinds of things. Its hard to change each and every file. How can I stop VS from giving these kinds of errors?
Seems you're using Stylecop on a non-compliant solution.
Create a setting file 'Settings.StyleCop' in the directory containing the *.sln file.
Add the following contents to the file:
<StyleCopSettings Version="105">
<GlobalSettings>
<BooleanProperty Name="RulesEnabledByDefault">False</BooleanProperty>
</GlobalSettings>
</StyleCopSettings>
This is either caused by a plugin like Resharper or StyleCop. You can turn off some of these warnings or disable the plugin completely.
Related
I'm converting my project to .NET 6 and I want to use filescoped namespaces everywhere. But the conversion tool exists only in the editor.
Has anyone found out if there's a way to run this editor function across all files in solution at once? (Looks like Rider has that function)
Adding a rule to use file scoped namespaces in .editorconfig worked for me:
create an .editorconfig file in the solution directory
add following line/content below (docs, code - IDE0161)
Example .editorconfig file content:
[*.cs]
csharp_style_namespace_declarations = file_scoped:warning
After that the preview changes dialog had an option to apply the fix to the whole project/solution:
I always have problems finding files that are supposed to be updated (.editorconfig in this case). I don't even know if I should search for it in the project's, Visual Studio installation's or any folder on the PC. So I like the answer in the link below because it says where in the interface to change the setting.
Best answer in my opinion is here:
https://www.ilkayilknur.com/how-to-convert-block-scoped-namespacees-to-file-scoped-namespaces
It says that you can change the code-style preference (and enable the display of the option to apply this preference in a document / project / solution) by going to Tools => Options => Text Editor => C#=> Code Style and then changing the related preference.
EditorConfig syntax
csharp_style_namespace_declarations = file_scoped:error
dotnet_diagnostic.IDE0161.severity = error
Note
Syntax option = rule:severity will be deprecated, sooner or later.
I strongly recommend to read this article before you start build .editorconfig for your project.
After you have configured the .editorconfig, you can configure a 'Code Cleanup' setting to automatically convert all files to use file-scoped namespace. Go to Tools -> Options -> Text Editor -> Code Cleanup -> Configure Code Cleanup. Then add the 'Apply namespace preferences'. Then go to Analyze -> Code Cleanup (or just search for 'Code cleanup') and run the Code Cleanup to automatically change the namespaces to file-scoped.
How to hide the ... that appeared everywhere in Visual Studio 16.10?
e.g.
It appears for unused usings, expression value not assigned, and many more.
[I'm NOT asking how to turn off graying out of unused usings.]
Dots like that, along with squiggly underlines, are coming from diagnostics issued by the compiler and analyzers.
Each diagnostic has its own identifier code such as CS1234 or IDE1234, etc.
You can configure the severity of each of these as described here:
https://learn.microsoft.com/en-us/visualstudio/code-quality/use-roslyn-analyzers?view=vs-2019
The easiest way is to add/edit your .editorconfig file and put the following in:
dotnet_diagnostic.<rule ID>.severity = <severity>
Where <rule ID> is the diagnostic ID, and <severity> is one of none, silent, suggestion, warning or error.
This gives you very fine grained control over these kinds of tips in your solution.
If you put the .editorconfig file in the root of your repo, it will apply to all projects in the solution.
Here's an example of using the lightbulb menu (Ctrl+.) to configure the severity of a diagnostic with code VSTHRD002:
There's also a section "Suppress ..." which lets you suppress a specific instance of the diagnostic, in contrast to changing the severity which applies across a broader scope.
Note you have have multiple .editorconfig files. For example, if all your unit tests are under a test folder, you could drop an .editorconfig file in there that has more relaxed rules.
I have a solution that includes various C# projects, and I just now noticed that I can't change the default namespace on any of them.
When I go to Properties --> Application on any of the project and change the "Default Namespace" field, a fatal error dialog pops up that says:
exception of type 'system.runtime.interopservices.externalexception' was thrown
At this point Visual Studio becomes unusable, as I can't close it or leave the screen, every click or button combination I press makes the same dialog pop up. The only thing I can do is terminate VS with the task manager.
I got around it by just renaming the <RootNamespace> node in the .csproj file
Maybe you should try to do this in another way:
Ctrl-H - Find: PreviousNameSpaceName Replace: NewNamespaceName
You can also right click the namespace in code and Refactor->Rename.
What is more: you can change the names of your projects, solutions, directories - etc. Namespaces, when adding new files, are generated using these names in fact.
I had to investigate the issue in our company and I found another workaround in our case. So I think it make sense to add it here.
Possible Solution
Check if you have more than one suo-File for the project/solution and delete the one which doesn't has the ".v12.suo" naming (better delete both)
Story behind:
We still have two Visual Studio installed (2010 and 2013) and some of our developer worked recently on 2010. So they had on the file system a regular ".suo"-File. Afterwards (some weeks later) they started to use 2013 (which created a ".v12.suo"-File).
We renamed both suo files, started VS2013 (which created the v12.suo) and were able to rename the namespace without a crash. In addition we had this issue not on all solutions. I didn't see yet a pattern, but in our case it happened mostly on solution with at least two projects and referenced project-libraries.
EDIT I started a new console application to see if the features in question would work, and they did flawlessly. I guess the issue is now why are they not working when I check out file for edit in TFS?
I just downloaded Visual Studio 2012 and after some digging I am still having some issues. Visual Studio is not giving me the errors (red swiggly lines under the code) where they should be. I made a bogus using statement to illustrate:
The last using statement obviously isn't valid, so why isn't it telling me so?
Another example:
public String method()
{
someVar = "hi";
}
Even though I have no someVar variable set anywhere within scope, there is no error. I am using classes obtained via T.F.S., though I doubt that matters but just in case it does I thought I would mention it.
It should also be noted that I do have intellisense capabilities enabled yet those are not functioning either.
How can I fix this madness :-)?
I guess you have not opened any project. You just opened a C# source file. Visual Studio can't provide intellisense and error detection outside of a project, except some obvious errors (like missing a semicolon).
The easiest way to determine the ownership of the blame in this situation is to open:
Tools -> Options -> Text Editor -> C# -> Advanced
If "Underline errors in the editor" and "Show live semantic errors" are checked, you likely are looking at a file that is not marked to compile, or isn't a part of the current solution.
If they aren't checked, one of your plugins is likely disabling them i.e.
ReSharper > Options > Code Inspection > Settings > Enable Code Analysis
Ok, we have a 'pseudo-c#' file that we've added to the solution for reference. While it does have a .cs extension (to let VS take a stab at color-coding it since it's pretty big and that helps with readability) the file itself isn't marked to be compiled with the action set to 'none'.
Now, when we do a full build, everything builds just fine and the file is ignored as it's supposed to be. However, the error window is showing all the errors from the faux code. We of course can change the extension which gets rid of the errors, but then we lose syntax coloring.
Now I'd assume since we explicitly marked that file as 'do not compile' and since the errors are actually ignored by the compiler, they wouldn't show up, but obviously that isn't the case.
So anyone have any idea on how to hide errors that are generated in files that are marked to be excluded from the compiler?
Update:
Because people keep suggesting work-arounds, I want to be clear that we're already using work-arounds. That's not what I'm after here. I'm trying to find out specifically if there is some setting or feature in VS that we can enable/disable that says 'Hey, you're not being compiled so you don't get to be in the error list!' Not sure there even is such a thing, but if so, that's what I'm trying to find.
You can:
Exclude the file from your project. Right click the file in question, and choose "Exclude". To view this file, select "Show all files" at the top of your Solution Explorer, and you can edit your code with full syntax highlighting.
Use a preprocessor #ifdef to conditionally include and exclude blocks (or files) of code. Note that non-included blocks of code will not be highlighted (and checked for errors).
One workaround could be to exclude the file from the project and ensure 'show all files' is on in visual studio. That way you will be able to see the file and still get syntax highlighting when you open it.