28

In my app I have a fair number of entities which have fields which are getting their values set via reflection. (In this case NHibernate is setting them). I'd like to get rid of the "x is never assigned to and will always have its default value 0" warnings, so I can more easily pick out the other warnings. I realize you can surround them in pragma directives, but AFAIK you have to do this for each one. Is there a project wide or solution wide way I could do this?

Luke Girvin
  • 13,221
  • 9
  • 64
  • 84
pondermatic
  • 6,453
  • 10
  • 48
  • 63
  • Any reason you wouldn't just put #pragma warning disable 169 at the top of the source code file? It suppresses for the remainder of the file. – Hans Passant Feb 08 '09 at 22:02
  • ++ to that, much better since it makes it clear that this file contains loads of these and not absolutely everything – ShuggyCoUk Feb 09 '09 at 13:54
  • 1
    In my case I'm using NHibernate to set the IDs of entities. So on every domain object I have private int _ID; I really don't want to have to set put pragma directives on each of the entites, but would rather something global. – pondermatic Feb 10 '09 at 01:56
  • ^^^ not only you ***should*** put the pragma directive in each entity, you should actually enable it and disable it on each individual offending assignment in each entity. Stop confusing "less typing" with convenience. Any convenience earned by less typing is paid for later with much more inconvenience and weeping and gnashing of teeth. – Mike Nakis Nov 13 '20 at 11:25

4 Answers4

39

Use the C# commandline option /nowarn http://msdn.microsoft.com/en-us/library/7f28x9z3(VS.80).aspx

To do this within visual studio goto Project properties->Build->(Errors and warnings) Suppress Warnings and then specify a comma separated list of warnings which need to be suppressed.

Sandeep Datta
  • 28,607
  • 15
  • 70
  • 90
  • 15
    As AnthonyWJones points out below, the compiler warning IDs you need to put into this comma separate list are found in the Output window while viewing the Build stream.What isn't mentioned yet in this thread is that they come in the form `CSxxxx` and you must remove the 'CS' prefix in your listed codes in order for it to work. – J T R Sep 04 '12 at 02:42
  • 2
    This was hard to find in VS2017. In the menu bar choose **Project->(project name) Properties...** Choose **Build** tab. Look under **Errors and warnings** for **Suppress warnings:** Add the warning numbers in that box. – wallyk May 08 '18 at 00:06
12

Open the project properties, on the build tab, enter warning IDs you want to surpress in the Suppress warnings: box.

AnthonyWJones
  • 187,081
  • 35
  • 232
  • 306
1

The VC++ XML tag for this is <DisableSpecificWarnings/> with a semi-colon separated list of numeric IDs. This doesn't appear to be documented anywhere that I can see but FYI.

Andy Piper
  • 563
  • 4
  • 10
0

At least in VS2019, you can control solution-level (truly, it's directory-level) warnings in the Directory.Build.Props file by adding the following at the <Project> level:

    <PropertyGroup>
        <NoWarn>MSB3270;MSD3246</NoWarn>
    </PropertyGroup>
Isaac Baker
  • 323
  • 2
  • 10