Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / August 2007

Tip: Looking for answers? Try searching our database.

Some questions on Raising events

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Dom - 22 Aug 2007 22:20 GMT
I'm teaching myself how to write a usercontrol, and how to raise
events within the control.  Some questions came up.  Let's say I the
event is called "Flyover", and everybody knows what is meant by
FlyoverEventArgs and OnFlyover.

1.  When the event is to be raised, as I understand it, I do not raise
it directly.  Instead I call a procedure "OnFlyover (new
FlyoverEventArgs (...))".  Then I write the procedure called
"OnFlyover" which then does nothing but raise the event -- "Flyover
(this, e)".  What is the purpose of "OnFlyover"?  It seems to be
nothing but a link in the chain.  It seems that instead of calling
this, I can just raise the event itself.  Is it only to allow other
users to override it?

2.  When I write FlyoverEventArgs, I always get an error about
accessibility, which can only be corrected if I make the class
"public".  I thought "public" was the default for a class.  Even the
IDE, when it creates a stub, does not insert the word "public".

3.  Once, when I wrote FlyoverEventArgs, I forgot that it was supposed
to extend the class "EventArgs".  But nothing went wrong.  Why are you
told to extend "EventArgs"?

All things considered, it seems that the IDE should have a special
insert for "EventArgs" that includes the word "public" and the
extension of "EventArgs" automatically in the stub.
Jon Skeet [C# MVP] - 22 Aug 2007 22:45 GMT
> I'm teaching myself how to write a usercontrol, and how to raise
> events within the control.  Some questions came up.  Let's say I the
[quoted text clipped - 9 lines]
> this, I can just raise the event itself.  Is it only to allow other
> users to override it?

It allows them to override it, or call it - and it also lets you
encapsulate any nullity checking, locking etc in one place.

> 2.  When I write FlyoverEventArgs, I always get an error about
> accessibility, which can only be corrected if I make the class
> "public".  I thought "public" was the default for a class.  Even the
> IDE, when it creates a stub, does not insert the word "public".

No - the default access modifier is always the most restricted it can
be in C# (with one exception when you change the access to half of a
property). This means that for outer class, the default access is
internal; for nested classes, the default access is private.

> 3.  Once, when I wrote FlyoverEventArgs, I forgot that it was supposed
> to extend the class "EventArgs".  But nothing went wrong.  Why are you
> told to extend "EventArgs"?

In C# 1, there was very little benefit in deriving from EventArgs. In
C# 2 and 3, however, delegate parameter contravariance means that if I
have a method:

void Foo (object sender, EventArgs e)

I can use that as the action for your delegate type, even though it's
declared to pass in FlyoverEventArgs - but only if FlyoverEventArgs
derives from EventArgs.

(There's nothing specific about EventArgs here, by the way - the
contravariance applied to delegates is general.)

> All things considered, it seems that the IDE should have a special
> insert for "EventArgs" that includes the word "public" and the
> extension of "EventArgs" automatically in the stub.

You may not want it to be public though - not all events are public.
Besides, it's pretty rare to need to create your own event argument
type - I don't see that there would be much benefit in changing the
IDE's behaviour for it.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Peter Duniho - 23 Aug 2007 01:00 GMT
> [...]
>> What is the purpose of "OnFlyover"?  It seems to be
[quoted text clipped - 4 lines]
> It allows them to override it, or call it - and it also lets you
> encapsulate any nullity checking, locking etc in one place.

And, correct me if I'm wrong (like I have to ask :) ), but...

The "event" being used in the handler isn't the same as the "event" used
publicly, right?  That is, the "It allows them to...call it" is very
important in the case where you want the event to be "raiseable" by a
derived class, since the derived class wouldn't have access to the
actual event instance member.

Pete
Jon Skeet [C# MVP] - 23 Aug 2007 07:33 GMT
> >> What is the purpose of "OnFlyover"?  It seems to be
> >> nothing but a link in the chain.  It seems that instead of calling
[quoted text clipped - 11 lines]
> derived class, since the derived class wouldn't have access to the
> actual event instance member.

Exactly. The member referenced within the class is actually the
delegate field, not the event.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.