为什么gridview事件处于保护模式 [英] Why gridview events are in protected mode

查看:70
本文介绍了为什么gridview事件处于保护模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么gridview事件处于保护模式有什么优势......



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

Why gridview events are in protected mode what is it advantages...

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

推荐答案

这句话是三重错误,如果不是更糟的话。



单词 protected 并不意味着任何模式;这是一个访问修饰符,意味着派生类的可访问性。事件永远或几乎从不受保护,它们是 public ,这非常重要,因为需要访问事件将事件处理程序添加到事件的调用列表中;事件的处理通常在不同类型的代码中完成,而不是声明事件的类型。最后,示例代码不是事件,但它可能是事件处理程序的示例。我不知道为什么这个方法被声明 protected ,最典型的是,这样的事件处理程序不必具有如此高的可访问性;它们应该是私有的。



现在,关于帖子中不那么重要的谬误:代码示例中显示的方法名称违反了(好的)Microsoft命名约定。难怪:它看起来像是一个自动生成的名称。这些名称不是直接使用的,应该重命名为语义上合理的名称。你觉得为什么给你重构引擎?自动生成的代码原则上不能是语义的,因此它们生成的名称很自然。



现在,关于一些真正的 protected 成员。在UI类中,许多事件都有自己的方法对应物。例如,有事件 System.Windows.Forms.Control.Paint 和方法 System.Windows.Forms.Control.OnPaint ;并且该方法调用该事件;很明显,该方法使用相同的事件参数参数,并且没有 sender 参数,当然,这不是必需的。为什么有这样的配对?这种对的方法部分通常是 protected ,事件几乎总是 public 。为什么?我解释了为什么公开。并且该方法受到保护,因为它旨在用于派生类。



另一个重要方面是:除了声明类之外,不能从任何地方调用事件。它甚至不能从直接派生的类中调用。这是一个重要的万无一失的功能。第二层保护是使用方法 protected ,以减少意外调用事件的几率。



你真的需要学习代表和活动。这些主题并不像第一眼看上去那么简单。在任何尝试开发任何UI或任何更高级的代码之前,我建议立即这样做。



-SA
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.

—SA


这篇关于为什么gridview事件处于保护模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆