Click here to Skip to main content
15,939,778 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Why gridview events are in protected mode what is it advantages...

eg : protected void grdTaskSubmit_RowUpdating(object sender, GridViewUpdateEventArgs e){}

1 solution

This statement is triple-wrong, if not worse.

The word protected does not mean any mode; this is an access modifier meaning accessibility from the derived classes. Events are never or almost never protected, they are public, which is very important, because access to events is needed to add event handlers to the events' invocation lists; and handling of the events is typically done in the code of different types, not the types declaring the events. And finally, the sample code is not an event, but it could be the example of an event handler. I have no idea why this very method is declared protected, most typically, such event handlers do not have to have such high accessibility; they should be private.

Now, about less important fallacies of your post: the method name shown in the code sample violates (good) Microsoft naming conventions. No wonder: it looks like an auto-generated name. Such names are not intended to be directly used and should be renamed to something semantically sensible. How do you think why are you given the refactoring engine? The auto-generated code could not be semantic in principle, so they generated names are quite natural.

And now, a side note about some really protected members. In UI classes, many events have their method counterparts. For example there are event System.Windows.Forms.Control.Paint and the method System.Windows.Forms.Control.OnPaint; and the method invokes the event; the method, quite obviously, uses the same event arguments parameters and no sender parameters, which is, of course, not needed. Why having such pairs? The method part of such pair is usually protected, and the event is almost always public. Why? I explained why public. And the method is protected because it is intended to be used in the derived class.

Another important aspect is: an event cannot be invoked from anywhere except the declaring class. It cannot be invoked even from the immediately derived class. This is the important fool-proof feature. The second-layer protection is having the method protected, to reduce the chances of the accidental invocation of the event.

You really need to learn delegates and events. These topics are not so simple as it may seem from the first glance. I would advise to do it immediately, before any attempts to develop any UI or any more advanced code.

Share this answer
Mohammed Shamsheer 22-Mar-14 0:26am    
Thank u nice advice from u...i love ur advice and answer...
Sergey Alexandrovich Kryukov 22-Mar-14 0:52am    
You are very welcome. I appreciate your constructive approach and understanding.
Good luck, call again.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900