事件参数 [英] event arguments

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

问题描述

对于很多人来说这可能听起来很愚蠢,但我在想,当我们能够使用对象为什么我们真的需要事件args来传递任何函数时,例如


bool MyFunction(对象发送者,System.EventArgs e){}

我的问题更多地针对我们在编程时创建的自定义事件参数。

只是为了让它更具可读性或者可以集体制作

和层次结构?


也许这个问题这非常像我以前的帖子,为什么我们需要3

diff这种异常,当他们没有做任何好事。基本上所有都是

System.Exception。


System.Exception

System.SystemException

系统.ApplicationException


并且,如果有ApplicationException那么可能会有什么损害添加

每次说出ConsoleException或WindowsApplicatoinException。


根据以下链接(感谢Jay Harlow)
http://blogs.msdn.com/brada/archive/.../25/96251.aspx


好​​像没有人喜欢使用ApplicationException。


如果这是一般的感觉,为什么现在只使用


bool MyFunction(对象发送者,MyObject myObj) {}


我的观点是,为什么连参数名称空间无论如何!


谢谢,

This might sound little stupid to many but I was thinking that when we can
use object why we really need event args to pass in any functions e.g.

bool MyFunction(object sender, System.EventArgs e){}

my question is targetted more on the customized event arguments that we
create while programming.
Is it just to make it more readable or maybe to make things in collective
and hierarchy?

Maybe this question it is very much like my previous post on why we need 3
diff kind of exception when they not doing any good. Basically all are
System.Exception.

System.Exception
System.SystemException
System.ApplicationException

And, if ApplicationException is there then what could have hurt adding
ConsoleException or WindowsApplicatoinException per say.

As per the following link (thanks to Jay Harlow)
http://blogs.msdn.com/brada/archive/.../25/96251.aspx

seems like no one likes to use ApplicationException.

If this is the feeling in general than why now just use

bool MyFunction(object sender, MyObject myObj){}

My point is that why even argument namespace anyways !

Thanks,
Po

推荐答案

很大程度上,这是一个良好的面向对象设计的问题,以回答这两个问题你的

问题。


对于事件和事件处理程序,他们只是代表。你

可以为你自己的自定义事件处理程序传递任何东西。实际上,事件关键字的MSDN文档中的示例

有许多不同的

代表,这些代表具有不同的参数集


框架使用(对象发送者,EventArgs e)的事实是他们建立的

约定,它往往是一个相当好的系统。它可以很容易地处理框架中的事件,因为你不需要b $ b必须记住一堆完全不同的不同事件

参数。有一个既定的,易于记忆的模式。


至于例外,论点有点不同,但它基本上就像是

这:通常,您希望按类型对异常进行分类。你不希望
想要总是抛出一个ApplicationException或Exception。如果传递给方法的参数无效,那么你想抛出一个InvalidArgumentException,

或IO问题的IO异常。


这允许您根据异常的分类在适当的地方组织处理。

。它还允许您按组处理

异常。例如,如果您收到某种

异常,则可能需要向用户显示消息。该消息可能是异常中包含的
。对于不同类型的异常,您可能只想将其记录到文件中,但不会通知用户异常。


再次,它只是一个好的OO设计问题。


Pete


Pohihihi < PO ****** @ hotmail.com>在消息中写道

新闻:ui ************* @ TK2MSFTNGP15.phx.gbl ...
Largely it''s a matter of good object oriented design, in answer to both your
questions.

In the case of events and event handlers, they''re simply delegates. You
could pass anything for your own custom event handlers. In fact the samples
in the MSDN documentation for the event keyword has a number of different
delegates with different sets of parameters

The fact that the framework uses (object sender, EventArgs e) is a
convention they established and it tends to be a fairly good system. It
makes it pretty easy to deal with events in the framework because you don''t
have to remember a bunch of different events with completely different
arguments. There''s an established, easy to remember pattern.

As for exceptions, the argument is a bit different, but it''s basically like
this: In general, you want to classify your exceptions by type. You don''t
want to always throw an ApplicationException or Exception. You want to throw
an InvalidArgumentException if the arguments passed to a method are invalid,
or an IO based exception for IO problems.

This allows you to organize the handling in places that are appropriate,
based on the classification of the exception. It also allows you to handle
exceptions in terms of a group. For example, if you receive on kind of
exception, you may want to show a message to the user. The message may be
contained in the exception. For a different type of exception, you might
just want to log it to a file, but not notify the user of the exception.

Again, it''s simply a matter of good OO design.

Pete

"Pohihihi" <po******@hotmail.com> wrote in message
news:ui*************@TK2MSFTNGP15.phx.gbl...
这听起来有点愚蠢到很多,但我在想,当我们能够使用对象时为什么我们真的需要事件args传递任何函数,例如

bool MyFunction(object sender,System.EventArgs e){} 是否只是为了使其更具可读性或者可以集体制作
和层次结构?

也许这个问题非常像我以前的帖子,为什么我们需要3
差异,当他们没有做任何好事。基本上都是
System.Exception。

System.Exception
System.SystemException
System.ApplicationException

如果是ApplicationException,那么然后可能会有什么损害添加
ConsoleException或WindowsApplicatoinException。根据以下链接(感谢Jay Harlow)
http://blogs.msdn.com/brada/archive/.../25/96251 .aspx

似乎没有人喜欢使用ApplicationException。

如果这是一般的感觉,为什么现在只使用
bool MyFunction(对象发送者,MyObject myObj){}

我的观点是为什么甚至参数名称空间反正!

谢谢,
Po
This might sound little stupid to many but I was thinking that when we can
use object why we really need event args to pass in any functions e.g.

bool MyFunction(object sender, System.EventArgs e){}

my question is targetted more on the customized event arguments that we
create while programming.
Is it just to make it more readable or maybe to make things in collective
and hierarchy?

Maybe this question it is very much like my previous post on why we need 3
diff kind of exception when they not doing any good. Basically all are
System.Exception.

System.Exception
System.SystemException
System.ApplicationException

And, if ApplicationException is there then what could have hurt adding
ConsoleException or WindowsApplicatoinException per say.

As per the following link (thanks to Jay Harlow)
http://blogs.msdn.com/brada/archive/.../25/96251.aspx

seems like no one likes to use ApplicationException.

If this is the feeling in general than why now just use

bool MyFunction(object sender, MyObject myObj){}

My point is that why even argument namespace anyways !

Thanks,
Po



>这对许多人来说可能听起来有些愚蠢,但我想当我们能够
> This might sound little stupid to many but I was thinking that when we can
使用对象时为什么我们真的需要事件args来传递任何函数,例如


有些活动不需要任何其他信息。因此,EventArgs的基类

没有属性。但有些活动可以利用发件人可获得的大量

信息。所以,你可以创建继承的EventArgs

类,并传递它们,CLR也有很多类。


至于为什么EventArgs是当没有需要任何信息时发送,好吧,那是'
约定。您可能想也可能不想使用它。微软确实如此,并且他们有理由。它确实在编码中创造了一致性,这总是一件好事。

也许这个问题非常像我以前的文章,为什么我们需要3 System.Exception。


即使是Exception类的类型也会为您提供有关

异常的一些信息,例如它来自何处。显然,没有其他信息需要




-

HTH,


Kevin Spencer

Microsoft MVP

..Net开发人员

复杂的东西由
组成
很多简单的事情。


" Pohihihi" < PO ****** @ hotmail.com>在消息中写道

新闻:ui ************* @ TK2MSFTNGP15.phx.gbl ...这对许多人来说听起来有点愚蠢,但我在想我们什么时候可以
使用对象为什么我们真的需要事件args来传递任何函数,例如

bool MyFunction(对象发送者,System.EventArgs e){}

我的问题更多的是针对我们在编程时创建的自定义事件参数。
是否只是为了使其更具可读性或者可以在集体和层次结构中创建?
差异,当他们没有做任何好事。基本上都是
System.Exception。

System.Exception
System.SystemException
System.ApplicationException

如果是ApplicationException,那么然后可能会有什么损害添加
ConsoleException或WindowsApplicatoinException。根据以下链接(感谢Jay Harlow)
http://blogs.msdn.com/brada/archive/.../25/96251 .aspx

似乎没有人喜欢使用ApplicationException。

如果这是一般的感觉,为什么现在只使用
bool MyFunction(对象发送者,MyObject myObj){}

我的观点是为什么甚至参数名称空间反正!

谢谢,
Po
use object why we really need event args to pass in any functions e.g.
Some events don''t require any additional information. Hence, the base class
for EventArgs has no properties. But some events can make use of lots of
information available to the sender. So, you can create inherited EventArgs
classes, and pass them, and the CLR has quite a few of them as well.

As for why EventArgs are sent when no information is needed, well, that''s a
convention. You may or may not want to use it. Microsoft does, and they have
their reasons. It does create a uniformity in the coding, which is always a
good thing.
Maybe this question it is very much like my previous post on why we need 3
diff kind of exception when they not doing any good. Basically all are
System.Exception.
Even the type of an Exception class gives you some information about the
exception, like where it came from. Apparently, no other information is
needed.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Complex things are made up of
Lots of simple things.

"Pohihihi" <po******@hotmail.com> wrote in message
news:ui*************@TK2MSFTNGP15.phx.gbl... This might sound little stupid to many but I was thinking that when we can
use object why we really need event args to pass in any functions e.g.

bool MyFunction(object sender, System.EventArgs e){}

my question is targetted more on the customized event arguments that we
create while programming.
Is it just to make it more readable or maybe to make things in collective
and hierarchy?

Maybe this question it is very much like my previous post on why we need 3
diff kind of exception when they not doing any good. Basically all are
System.Exception.

System.Exception
System.SystemException
System.ApplicationException

And, if ApplicationException is there then what could have hurt adding
ConsoleException or WindowsApplicatoinException per say.

As per the following link (thanks to Jay Harlow)
http://blogs.msdn.com/brada/archive/.../25/96251.aspx

seems like no one likes to use ApplicationException.

If this is the feeling in general than why now just use

bool MyFunction(object sender, MyObject myObj){}

My point is that why even argument namespace anyways !

Thanks,
Po



Kevin Spencer< ke *** @ DIESPAMMERSDIEtakempis.com>写道:
Kevin Spencer <ke***@DIESPAMMERSDIEtakempis.com> wrote:
这对许多人来说可能听起来有点愚蠢,但我在想,当我们能够使用对象时为什么我们真的需要事件args来传递任何函数例如
This might sound little stupid to many but I was thinking that when we can
use object why we really need event args to pass in any functions e.g.



有些活动不需要任何其他信息。因此,EventArgs的基类没有属性。但有些活动可以利用发件人可用的大量信息。因此,您可以创建继承的EventArgs类,并传递它们,CLR也有很多类。

至于为何在不需要任何信息时发送EventArgs,那么那是'约定'。您可能想也可能不想使用它。微软确实如此,他们有他们的理由。它确实在编码中创造了一致性,这总是一件好事。



Some events don''t require any additional information. Hence, the base class
for EventArgs has no properties. But some events can make use of lots of
information available to the sender. So, you can create inherited EventArgs
classes, and pass them, and the CLR has quite a few of them as well.

As for why EventArgs are sent when no information is needed, well, that''s a
convention. You may or may not want to use it. Microsoft does, and they have
their reasons. It does create a uniformity in the coding, which is always a
good thing.




在这种情况下,我从来没有理解过这个论点。如果有什么东西不需要b $ b b需要任何信息,那就不要提供论据。我们不建议

人们对其他方法调用有不必要的参数,那么为什么

为事件处理呢?


公约应该总是受到质疑 - 公约

来自MS的事实并不是一个好的。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet 博客: http://www.msmvps.com/jon.skeet

如果回复小组,请不要给我发邮件



I''ve never understood this argument in this case. If something doesn''t
need any information, just don''t provide an argument. We don''t suggest
that people have an unnecessary argument for other method calls, so why
do it for event handling?

Conventions should always be questioned - the fact that the convention
comes from MS doesn''t make it a good one.

--
Jon Skeet - <sk***@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

这篇关于事件参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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