views:

537

answers:

4

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?

A: 

The compiler is apparently not aware that it's being used in XAML code. Try suppressing the warning in your event definition.

Also, make sure you're actually raising the event somewhere.

SLaks
That's what I thought too, so I moved the XAML code to the code behind and it showed the same warning!And yes, I'm 100% sure the event is being raised somewhere. I'm looking right at it. It's wired to a button.
sfjedi
A: 

You can supress individual warnings.

\Program.cs(13,20): warning CS0219: The variable 'foo' is assigned but its value is never used

In this case, CS0219 is the warning regarding variables being assigned but not used. You can either use the /nowarn:0219 flag, or add the error number in the properties pane for the project (under "Build", remember to remove the leading CS). Keep in mind the supresses all warnings of this class.

Svend
Good to know! Thanks :)
sfjedi
+9  A: 

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.

lc
That's exactly what I need! Thank you!The only difference is that I added my own comment beside the 67 so I knew what it was in the future. Here's "exactly" what I typed...#pragma warning disable 67 // event never usedpublic event RoutedEventHandler SearchClick;#pragma warning restore 67
sfjedi
A: 

If you are forced to imlpement an event from an interface, that your implementation doesn't need you can do the following to avoid the warning.

public event EventHandler CanExecuteChanged { add{} remove{}}

Thejuan