How to disable compilation warnings in Visual Studio

0.00 avg. rating (0% score) - 0 votes

Retrieving the warning ID:


The warning ID can be taken from the Build Output. In the following example, the warning ID is 0169 (we only need the numeric part).

test.cs(17,9): warning CS0169: The private field ‘foo.c’ is never used


VB.NET projects: By modifying project file (.vbproj)


To suppress this warning in VB .NET we need to manually edit project .vbproj file.

  1. Close VS.
  2. Open .vbproj file. Find tag and add the warning ID there separated by commas. Don’t place any whitespaces and newlines in this tag! There is one section for each configuration (Release/Debug) so you need to edit all of them.
  3. Save the .vbproj file and open VS.

After this, you shouldn’tt get specified warnings during the build nor should you see warning underline during editing.


Examples:


(1) Type is not CLS-compliant


A variable, property, or function return is declared with a data type that is not CLS-compliant.

For an application to be compliant with theCommon Language Specification (CLS), it must use only CLS-compliant types.

To disable these warnings, use the following warning numbers: 40025,40026,40027,40028


Reference: How to disable ‘not CLS-compliant’ warnings?, http://www.vbforums.com/archive/index.php/t-481759.html


(2) XML-comment related warnings

XML comment block cannot be associated with any language element that supports the application of XML documentation comments. XML comment will be ignored.


XML comment has a tag with a ‘cref’ attribute ‘^abc.txt’ that could not be resolved. XML comment will be ignored.


Use 42312,42309


Reference: http://www.helixoft.com/vsdocman-faqs/disable-warnings-for-xml-comments.html


VB.NET projects: using Visual Studio Team Suite:


In the Project Properties > Code Analysis section, expand your warning messages and uncheck the one you don’t want to show up.


Reference: http://www.vbforums.com/archive/index.php/t-481759.html


C# projects: change build properties

To disable warnings, add the warning numbers separated by comma to the Suppress Warnings text box under Project Properties/Build:


Suppress warnings from code


In C#:


A pretty cool feature in .NET 2.0 is the ability knowingly disable a list of warnings using the #pragma directive.


using System;
public class foo
{
// Disable warnings for unused or unassigned fields
#pragma warning disable 0169, 0414
// No warning
int a;
// No warning
int b = 0;
// Restore warnings for unused or unassigned fields
#pragma warning restore 0169, 0414
// Warning CS0169 issued
int c;
// Warning CS0414 issued
int d = 0;
}


Reference: http://blogs.msdn.com/kaevans/archive/2005/11/06/489681.aspx


In C++:


// Disable warning messages 4507 and 4034.
#pragma warning( disable : 4507 34 )

// Issue warning 4385 only once.
#pragma warning( once : 4385 )

// Report warning 4164 as an error.
#pragma warning( error : 164 )

// Or all the above 3 in one line:
#pragma warning( disable : 4507 34; once : 4385; error : 164 )


Reference: http://msdn.microsoft.com/en-us/library/2c8f766e%28VS.80%29.aspx

0.00 avg. rating (0% score) - 0 votes
ToughDev

ToughDev

A tough developer who likes to work on just about anything, from software development to electronics, and share his knowledge with the rest of the world.

Leave a Reply