Monday, September 27, 2010

C# Warning CS0067: The event 'event' is never used


In my current project I am using a library that is delivered to me. The API in this library provide an interface that is a bit big! The interface requires my class to implement two events as follows:

#region INotifyPropertyChanged Members
public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

#endregion
#region INotifyPropertyChanging Members
public event System.ComponentModel.PropertyChangingEventHandler PropertyChanging;




These two events have no meaning in my class and I just have to implement them for the sake of the interface.

As I start to compile my code I see the Warning CS0067 that indicates that my events are not used anywhere, which is true.



As usual, I don't want my code gets spoiled with warnings en I would like to sort out ALL MY WARNINGS. So I came across some options:

  • Ignoring this warnig throughout the project.

    This is too much, I don't want to avoid the benefit of getting this nice warning that just highlighted my problem. But if I would like to do so I need to put the number 0067 in the project properties page in Build tab onder the Supress warnings

  • The less wide solution would be to ignore this warning only in that file and I could do this by putting the code just before my declaration:

    #pragma warning disable 0067

    and then restore it after my declaration using
    #pragma warning restore 0067



  • The last option I just found from MSDN which seems a neat solution is to tell the compiler that I am deliberately not supporting this event and even if it would be called at runtime that would be a mistake.

    To do so I need to throw an exception as follows:
    public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged
    {
    add { throw new NotSupportedException(); }
    remove { }
    }

    public event System.ComponentModel.PropertyChangingEventHandler PropertyChanging
    {
    add { throw new NotSupportedException(); }
    remove { }
    }





13 comments:

web marketing melbourne said...

You make a great point. Got some great information here. I think that if more people thought about it that way, theyd have a better time understanding the issue. Your view is definitely something Id like to see more of. Thanks for this blog. Its fantastic and so is what youve got to say.

Anonymous said...

It`s really nice article. Thank u a lot

Anonymous said...

Mooie post. Bedankt

Anonymous said...

Rather interesting to reed. Thx u . U make me happy

Anonymous said...

Awesome, that’s exactly what I was scanning for! You just spared me alot of searching around

Anonymous said...

hi, new to the site, thanks.

allex said...

Wow, just what information I need it.
I spent hours to find Attribute which can supress CS0067 warning message not found nothing.

ASFAIK, the last option is best, since it obviously declares PropertyChange is not supported, and make code more readable.

Anonymous said...

Hi,

Thanks for sharing this link - but unfortunately it seems to be down? Does anybody here at wdevs.blogspot.com have a mirror or another source?


Thanks,
James

Anonymous said...

Hi - I am definitely happy to discover this. great job!

Anonymous said...

Hey - I am really glad to discover this. great job!

Anonymous said...

Greetings! I know this is kinda off topic but I was wondering if you knew where I could get a captcha plugin for my comment form? I'm using the same blog platform as yours and I'm having trouble finding one? Thanks a lot!

Asghar Panahy said...

You could use http://captcha.codeplex.com/

or, generate a picture yourself using a handler,

or many other.
I implemented my own image generator using a handler. It is not water proof but it does do the job...so far :-)

Anonymous said...

I am asking for my mother. She doesn't necessarily want to make money off them, her purpose is to use her blog (once popular) and use it as references to possibly help her get a newspaper article. She has a title for one called "Answers to Life's Problems". Where can she post blogs and they become popular? She posted it already on WordPress but there are 3 million people posting blogs hers gets lost in the mix. Any suggestions?.

PoIuYt