Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to suppress compiler warning for specific function in VS2005 (VB.Net)

I have a class that inherits from a base class and implements the following...

    Public Function CompareTo(ByVal obj As Object) As Integer Implements System.IComparable.CompareTo

Now the base class it inherits from also implements this System.IComparable.CompareTo so I'm getting the following compiler warning:

Warning: 'System.IComparable.CompareTo' is already implemented by the base class. Re-implementation of function assumed.

I'm fine with that so my question is how can I suppress this warning for just this function (i.e. not all such warnings).

Clarifications:

  • Here is a link to the error on MSDN.
  • I've already tried both Shadows and Overrides and neither eliminates the warning.
  • The warning isn't on the method itself (unless Shadows or Overrides are omitted), but rather it's on "Implements System.IComparable.CompareTo" specifically.
  • I am not looking to suppress all warnings of this type (if they crop up), just this one.

Solution:
I was hoping to use the System.Diagnostics.CodeAnalysis.SuppressMessage attribute or something like C#'s #pragma but looks like there's no way to suppress the warning for a single line. There is a way to turn this message off for this project though, without turning all warnings off.

I manually edited the .vbproj file and included 42015 in the node for Debug and Release compilations. Not ideal but better than always seeing the warning in the IDE.

If someone has a better solution please add it and I'll gladly try it flag the answer.

like image 630
Sean Gough Avatar asked Nov 07 '08 18:11

Sean Gough


People also ask

How do I stop compiler warnings?

Some warnings are suppressed by default. To override this, use the --strict_warnings switch to enable all suppressed warnings. By default optimization messages, that is most of the messages between 1593 and 2159, are not warnings. To treat optimization messages as warnings, use the --diag_warning=optimizations option.

How do you suppress code Analysis warnings?

You can suppress violations in code using a preprocessor directive, the #pragma warning (C#) or Disable (Visual Basic) directive to suppress the warning for only a specific line of code. Or, you can use the SuppressMessage attribute.

How do I turn on compiler warnings in Visual Studio?

If you want to turn it on (or off) in the project setting, you have to go to: Configuration Properties -> C/C++ -> Command Line and then under Additional Options you can enter: /w3#### to set your warning to level 3, and thus enable it; or you can enter /wd#### to disable a warning.


2 Answers

Only use 'Implements' in the base class:

Signature in the base class:

Public Overridable Function CompareTo(ByVal obj As Object) As Integer Implements System.IComparable.CompareTo

Signature in the inherited class:

Public Overrides Function CompareTo(ByVal obj As Object) As Integer
like image 157
Topi Avatar answered Nov 15 '22 06:11

Topi


You can use the supress warnings just to suppress one warning. See here for more on how to. You can also use the SuppressMessage Attribute.

like image 31
Bruno Shine Avatar answered Nov 15 '22 08:11

Bruno Shine