Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get rid of "[some event] never used" compiler warnings in Visual Studio?

For example, I get this compiler warning,

The event 'Company.SomeControl.SearchClick' is never used.

But I know that it's used because commenting it out throws me like 20 new warnings of XAML pages that are trying to use this event!

What gives? Is there a trick to get rid of this warning?

like image 631
jedmao Avatar asked Jul 07 '09 16:07

jedmao


People also ask

How do I stop compiler error?

An error, by its nature, is indicating that the compiler believes it cannot generate valid code. The only way to suppress errors is to fix them. Just add the return statement it wants and then raise an issue on Microsoft Connect indicating that you believe the compiler is getting this one wrong.

How do I ignore warnings in C#?

Use a #pragma warning (C#) or Disable (Visual Basic) directive to suppress the warning for only a specific line of code.

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.


1 Answers

This appears to be warning 67 and can thus be suppressed with:

#pragma warning disable 67 

Don't forget to restore it as soon as possible (after the event declaration) with:

#pragma warning restore 67 

However, I'd check again and make sure you're raising the event somewhere, not just subscribing to it. The fact that the compiler spits out 20 warnings and not 20 errors when you comment out the event is also suspicious...

There's also an interesting article about this warning and specifically how it applies to interfaces; there's a good suggestion on how to deal with "unused" events. The important parts are:

The right answer is to be explicit about what you expect from the event, which in this case, is nothing:

public event EventHandler Unimportant {     add { }     remove { } } 

This will cleanly suppress the warning, as well as the extra compiler-generated implementation of a normal event. And as another added benefit, it prompts one to think about whether this do-nothing implementation is really the best implementation. For instance, if the event isn't so much unimportant as it is unsupported, such that clients that do rely on the functionality are likely to fail without it, it might be better to explicitly indicate the lack of support and fail fast by throwing an exception:

public event EventHandler Unsupported {     add { throw new NotSupportedException(); }     remove { } } 

Of course, an interface that can be usefully implemented without some parts of its functionality is sometimes an indication that the interface is not optimally cohesive and should be split into separate interfaces.

like image 194
lc. Avatar answered Oct 08 '22 05:10

lc.