Three gray dots under variable names in Visual Studio - c#

What do these three gray dots mean? I recently updated to Visual Studio 2017, and I haven't ever seen them in other versions of VS.

They indicate suggestion from Visual Studio to refactor your code to be simpler, more readable and efficient.
See Quick Actions and Refactorings section of the Visual Studio 2017 (version 15.0) Release Notes.

Just to make doubly clear for newish programmers, these are not in any sense error messages. They are merely suggestions for more "readable and efficient" code as #Oleksander states.
Some may not be the best advice for your application, unlike warning or error messages, which almost 100% are in my case at least.
I had suggestion in a Unity game engine C# application to make a variable "readonly". I'm not sure in my case it makes practical difference whether this is readonly or not, as in this case I think fairly obvious in the context: a private variable which is serialized and can be set in game's editor program, nobody likely to create new method in this game scene manager class to alter/assign this variable. Likewise it gave a suggestion to create property for a class private field, which I think extra code overkill.
At the least these are low priority messages to action.
Suppressing 3 Dot Symbols in Visual Studio
If you right click and read through one of these suggestions, you can find there is option to suppress these messages in global project file, with various optional parameters. So eg in my case, now I don't want any readonly message suggestions for whole project (welcome correct me anyone more experienced if this is bad idea!), so in the root of project, same level as .sln solution file, .csproj files, .git file in my case for C# project, it saves file called "GlobalSuppressions.cs". Root location and this exact file name is presumably crucial.
I guess if you want stop future coders / future you from fretting or spending time on these 3 dot messages when you think waste of time, could be useful.
In my case, to get rid of all "readonly" keyword suggestions in this project, code is (comments are VS auto comments):
// This file is used by Code Analysis to maintain SuppressMessage
// attributes that are applied to this project.
// Project-level suppressions either have no target or are given
// a specific target and scoped to a namespace, type, member, etc.
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0044:Add readonly modifier")]

Related

How do I set common values (abstract) for a C# solution in Visual Studio with multiple sub projects?

I have a solution with multiple projects each of which connects to the same DB and uses overlapping constant values that I would like to set somewhere instead of replicating manually. I have tried a variety of things online like making a custom class and linking projects to it, setting constants in a project config file (which doesn't exist like the guides claim), and so on. I've been unable to figure this out after more than an hour of searching and experimenting so if you have any ideas, let me know. The structure looks like this (the blue-underlined stuff are some of the projects in the list):
You can make another project under the solution to contain your class.
All the other projects can then reference that project, meaning the same functionality will be available in all the other projects without having to duplicate anything.
I will extend the previous correct answer with some more information.
Your solution structure is something to think very carefully as it is a combination of application design/architecture and leads to extensibillity, scalability and future maintainability.
Take for example the following article Common web application architectures.
You can see the Clean Architecture (AKA Hexagonal) which leads to specific projects withing a solution
You can see older designs where the DB access would go into a project called ..DAL
Simple projects can use the second one, more business rich ones the first or something in between.
Check this this article on shared code projects to see about net standard projects
So the above was helpful, but far more complicated than it needed to be. Apparently other answers I'd seen actually work, but it took reading a bunch of other pages to figure out the whole puzzle. The working steps are:
Create a class with public parameters for your constants
Place that class somewhere in your solution space. When I created it on the solution, it was placed in "Solution Items" in my tree (which is the root folder of the solution on the file system).
Right click each project and ADD>Existing Item and point to the class. The KEY (that was missing from most things I read) was that the "add" button" has a drop-down arrow that lets you change it to "Add as link"
In each project (after adding as link to the file), you can directly reference the values as NAMEOFCLASS.NAMEOFCONST but ONLY if you declared them as public const SOMETYPE SOMENAME. Without the const, it's not able to directly reference the value
Note that this fix is in the .sln file itself and needs to be part of the commit or it won't have any effect. It would be nice if you could use "include" or something to bring in a file a folder one level up, but here we are.

Code analysis warnings in generated code (like migrations) - Visual Studio (C#)

Most of the warnings that currently I have at my project comes from code that was autogenerated. In most cases, it is (CA1062: Validate arguments of public methods). I noticed that even console hello word app generated by Visual Studio display warning (S1118: Utility classes should not have public constructors).
How I can disable warnings for autogenerated code? (migrations mostly)
As crgolden says, you can add // <auto-generated/> tag at the start of file. BUT I would be cautious with doing that to files where the generator tools chose to not add it. You specifically mention migrations - you are supposed to review them before commit, EF sometimes does surprising things (*). That review is also a good time to fix any warnings that bother you. If you care enough about the warning to leave it enabled, silencing it in migrations sets a bad precedent. And if you don't care about it, you can disable it globally.
My view of the auto-generated comment is that it belongs in files that are regenerated outside of your control, or frequently, or both. Since we use EF as example, ModelSnapshot is a good candidate, and the migration tools DO include it. <Migration>.Designer.cs files are also generated with it. And it should almost never be added by hand (**)
(*) delete one column and add another, you may find migration with rename action if they have similar types. Or your coleague did some change to code and forgot to generate migration for it - your migration will now include changes from both and his part will be untested.
(**) since every rule needs an exception, recently I was considering breaking this rule for a thirdparty library we imported into project as source files. In the end I decided to just set the whole project to warning level 0.
You can just add this comment to the top of the file:
// <auto-generated/>

How do I find all places that SET a property?

It is easy to find all code that uses a property, however how do I find the code that just sets it?
(When I do a “find all reference” on the “set”, it just does a “find all reference” on the property itself, including code that just reads it.)
You can use Resharper.
Alternately, set the setter to private (Or comment out the setter completely) and recompile. You will get errors where you're trying to set the property.
For what it's worth, this will be natively possible with VS2019.
Specifically the 'Find All References' window has a new 'Kind' column which can be filtered for 'Write' references:
The specific Github PR that added this feature is scheduled to be included in Visual Studio 2019 Preview 2 (16.0.P2) https://github.com/dotnet/roslyn/issues/22545
The full release of VS2019 is roadmapped for Q1 of 2019.
Try commenting the set part of property and build it gives error at all the places where it is used.
You could run a text search on propertyName = - you can try using regex search to allow for 0 to n spaces between the name and =.
AFAIK, this can't be done using the standard features of Visual Studio - it doesn't do anything special for properties to check whether they are being used on the left or right side when searching, and, to be sure, there's no option to tell it to do so.
To give an option without having to run extra regexes or install other software, you could just browse through the results window to let your eyes scan for left-side occurrences - maybe not the most productive but I'm not sure I see a great advantage over other suggestions.
Lastly, #Kamyar's suggestion to make the properties no longer accessible does seem worth a look, but this depends on how long it takes your project to compile, it could take even longer to find'em all - I'm not sure why you'd need Resharper to do this though.
Here's a fairly robust solution that'll work also for non-Properties using Visual Studio without 3rd party tools. Be sure to select the "Match Case" and "Use Regular Expressions" options in Find.
1. For all except Post-/Pre-fix Increment and Shift Assignments:
(^|[^\w.])MyVariable\s*([\+\-\*/%&|\^]|)=[\w\s]
2. For Post-/Pre-fix Increment and Shift Assignments:
((^|[^\w.])MyVariable\s*(\+\+|--)|(\+\+|--)\s*MyVariable[^\w.]|(^|[^\w.])MyVariable\s*(<<|>>)=)
3. For Out / Ref Parameters (N/A for Properties):
(^|[^\w.])(out|ref)\s+MyVariable[^\w.]
CAVEATS:
C#.NET only.
Visual Studio 2012+ only.
Does not work if "=" is followed by an EOL.
Does not work if "MyVariable" is followed by an EOL.
Depending on starting point and scope of the Find and scope of the Variable / Property, may find more / less references than necessary. When in doubt, error on side of "more", so you won't miss anything.
Does not work for "."-prefixed Variables / Properties.
6.1. Unless you include it as part of the "MyVariable" (i.e. "MyStructVariable.MyStructField" or "MyObjectVariable.MyObjectField") but you risk finding too few references since there may be other Struct or Object Variables used to make Assignments to the same Struct or Object Field or Property.

how to do a "Move type to another file to match its name" accross a complete solution

does anyone know of a way to split all classes in one solution into multiple files?
The point here is that I've inherited a project in which a few hundred files contain a thousand or so classes...
I'd like to be able to get to a 1 file per class approach..
Using resharper I can easily do this manually, but I'm guessing there must be a better way?
Kind regards
Frederik
You could try one of the ReSharper 5.0 nightly builds which allow you to do it across your whole solution. You can revert to ReSharper 4.5 (or whatever version you are using) afterwards.
GraemeF's answer is correct, but when you do that refactoring chances are you'll lose all source-control history for the existing classes. This might not be a problem for you (especially if the system you've inherited wasn't source-controlled!) but I've often found that line-annotated views of a class are very helpful for determining the intent behind a particular line.
ReSharper has the Ctrl-T shortcut to jump to a type name, and holding down Ctrl makes types clickable; that might be another way to solve your problem.

Odd problem unit testing internal classes with VS 2008

I'm starting a project which is broken up over multiple VS projects and I was planning on having separate testing projects for each of the projects, so I'd have a solution like this:
Project1
Project1.Test
Project2
Project2.Test
There are some internal classes which I want to have tested. So I used Visual Studio 2008 (SP1) to generate the test stubs in my test project and added the InternalsVisibleTo. But I get a red squiggly line under the internal class. If I compile I get a successful build, and looking at the test method the red squiggles are gone.
But if I tough the file the squiggles come back and I have no intellisense on the internal class.
The internal is within Project1 and the test is within Project1.Test. For completeness I decided to do the exact same manner of generating the test method but this time into Project2.Test, and this time it's shown to be completely working. I don't get red squiggles, I get intellisense, everything.
I've tried deleting Project1.Test and recreating the test method, everything I can think of, but no matter what I do I can't get the internal to be completely visible within its paired Test project, only in the one which is designed to be for another project.
It's doing my nut that it's not working!
I've seen this too, especially when using strong names. To be honest, I didn't get excited; as long as it compiles and tests correctly, I can live with the odd glitch. For example, if you get one build problem, I've seen it complain that it can't find the other (internal) methods - but a clean build shows no errors. Again, I'm not to bothered by this... (maybe I'm too forgiving?).
In particular, it is only rarely that I need to use an internal type/member in the tests (most of the time I'll try to test via the public API); so the lack of 100% reliable intellisense isn't usually a big problem. I already know the type/member I am looking for (copy/paste ;-p).
Sure, it would be nice if it was fixed, but if I was the budget manager, I could probably live with it and focus on other features first.
Could you be using a string constant or something other than the exact literal stirng (without concatenation) in your InternalsVisibleTo attribute? We had the habit of using a string constant to define it, and that works fine for everything except intellisense. Replace by pasting as a simple string and it works.
Deleting your .suo file (same folder as your solution file) might help as well.
It might be a problem with IntelliSense DB file. Try to delete it and have VS try and rebuild the DB.
To do this close the solution and delete (all?) .ncb files. To be on the safe side, just rename them to something like .nc4 or whatever. Reopen the solution and rebuild it. Let me know if it works.
EDIT: Apparently, ncb files are only for C++ projects. I don't know where the IntelliSense DB for C# projects are, nor could I find out. If I were you, I would still try to find a way to reset the DB.
Asaf

Categories

Resources