在C#和VB .NET中注册事件处理程序 [英] Registering Event Handlers in C# vs. VB .NET

查看:58
本文介绍了在C#和VB .NET中注册事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是否正确地认为因为C#没有Handles VB .NET的关键字

,我们必须在C#中手动注册事件代理,

而在VB .NET中使用Handles注册负责在幕后为我们注册活动

代表?


换句话说,*如果* C#确实有一个Handles-type关键字,我们还需要
需要注册一个事件代表吗?

Am I correct in thinking that because C# doesn''t have the "Handles" keyword
that VB .NET does, we have to register event delegates manually in C#,
whereas in VB .NET using "Handles" takes care of registering the event
delegate for us behind the scenes?

In other words, *if* C# did have a "Handles"-type keyword, would we still
need to register an event delegate?

推荐答案

Scott,

是的,你是对的,当你使用handles关键字时,VB将负责将代理分配给

事件。


我的假设不,你不必在C#中存在这样的事件来注册事件,但是这是不可能的,因为

有一直没有讨论它或提及任何有关它的事情

性质。


-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com

Scott M. < sm ** @ nospam.nospamwrote in message

news:Oc ************* @ TK2MSFTNGP06.phx.gbl ...
Scott,

Yes, you are right, VB will take care of assigning the delegate to the
event when you use the handles keyword.

My assumption is that no, you would not have to register the event if
something like that existed in C#, but it''s impossible to say, given that
there has been no discussion about it or mentioning of anything of that
nature.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Scott M." <sm**@nospam.nospamwrote in message
news:Oc*************@TK2MSFTNGP06.phx.gbl...

我认为因为C#没有VB .NET所做的Handles

关键字,我们必须手动注册事件代理
$ C $ b在C#中,而在VB .NET中使用Handles;负责在幕后为我们注册

活动代表?


换句话说,*如果* C#确实有一个Handles类型关键字,我们还需要

需要注册一个活动代表吗?
Am I correct in thinking that because C# doesn''t have the "Handles"
keyword that VB .NET does, we have to register event delegates manually
in C#, whereas in VB .NET using "Handles" takes care of registering the
event delegate for us behind the scenes?

In other words, *if* C# did have a "Handles"-type keyword, would we still
need to register an event delegate?



Ok Nicholas。我只是想确保我理解为什么我们不需要在VB .NET中使用它们(并且有一个Handles关键字)等同于我们为什么要这样做
必须用C#做(并且没有像Handles这样的东西)。


谢谢!


Nicholas Paldino [ .NET / C#MVP]" < mv*@spam.guard.caspershouse.com写在

消息新闻:uu ************** @ TK2MSFTNGP03.phx.gbl ...
Ok Nicholas. I just wanted to make sure my understanding of why we don''t
need to do it in VB .NET (and have a Handles keyword) equated to why we do
have to do it in C# (and don''t have something like Handles).

Thanks!

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:uu**************@TK2MSFTNGP03.phx.gbl...

Scott,


是的,你是对的,VB将负责将代表分配到
$ b使用handle关键字时$ b事件。


我的假设是,不,如果

存在于此类中,则不需要注册该事件C#,但这是不可能的,因为

没有讨论它或提及任何有关它的事情。


-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


" Scott M." < sm ** @ nospam.nospamwrote in message

news:Oc ************* @ TK2MSFTNGP06.phx.gbl ...
Scott,

Yes, you are right, VB will take care of assigning the delegate to the
event when you use the handles keyword.

My assumption is that no, you would not have to register the event if
something like that existed in C#, but it''s impossible to say, given that
there has been no discussion about it or mentioning of anything of that
nature.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Scott M." <sm**@nospam.nospamwrote in message
news:Oc*************@TK2MSFTNGP06.phx.gbl...

>我是否认为因为C#没有VB .NET所做的Handles关键字,我们必须手动注册事件代理活动代表?

换句话说,*如果* C#确实有一个Handles-type关键字,我们还会
需要注册一个活动代表吗?
>Am I correct in thinking that because C# doesn''t have the "Handles"
keyword that VB .NET does, we have to register event delegates manually
in C#, whereas in VB .NET using "Handles" takes care of registering the
event delegate for us behind the scenes?

In other words, *if* C# did have a "Handles"-type keyword, would we still
need to register an event delegate?




2008年1月2日星期三13:29:54 -0800,Scott M.< sm ** @nospam.nospamwrote:
On Wed, 02 Jan 2008 13:29:54 -0800, Scott M. <sm**@nospam.nospamwrote:

我认为是正确的,因为C#没有Handles

关键字
VB .NET的b $ b,我们必须在C#中手动注册事件代理,

而在VB .NET中使用Handles注册;负责在幕后为我们注册活动

代表?


换句话说,*如果* C#确实有一个Handles-type关键字,我们还需要
需要注册一个事件委托吗?
Am I correct in thinking that because C# doesn''t have the "Handles"
keyword
that VB .NET does, we have to register event delegates manually in C#,
whereas in VB .NET using "Handles" takes care of registering the event
delegate for us behind the scenes?

In other words, *if* C# did have a "Handles"-type keyword, would we still
need to register an event delegate?



正如尼古拉斯所说,很难说出可能是什么。


那就是说,我更喜欢C#方法。 VB隐藏了一些机制来自

你,恕我直言会让那些对整个代表感到陌生的人更加难以及为了真正理解发生了什么。


对于VB.NET程序员来说,抓住一个用于处理事件的方法可以应用于多个
这是一个特别困难的飞跃br />
事件,多个对象等以及给定事件可以有多个订阅者
因为处理事件的基本语法

VB.NET中的处理程序并没有暴露出来。


其他人的意见可能会有所不同。 VB通过在幕后推断行为或做事情来完成各种事情,为程序员提供顺畅的东西。我想有些人可能真的喜欢这个,但是每当我看到有人对VB.NET中发生的事情感到困惑时我就会感到很尴尬

他们是否会有如果VB.NET没有得到同样的困惑,那么代表程序员调用一些隐藏的行为(另一个最近的想法是VB.NET实例化的另一个例子一个Form派生类

实例作为某种默认实例使用,与先前创建的

冲突实例。

Pete

As Nicholas says, it''s hard to say what "might have been".

That said, I prefer the C# approach. VB hides a bit of the mechanism from
you, which IMHO makes it harder for people new to the whole delegates and
events thing to really understand what''s going on.

It can be an especially difficult leap for a VB.NET programmer to grasp
that a method written to handle an event can be applied to multiple
events, multiple objects, etc. as well as that a given event can have
multiple subscribers, because the basic syntax for dealing with event
handlers in VB.NET doesn''t expose that.

Other people''s opinions may vary. VB does a variety of things that smooth
things out for the programmer, by inferring behaviors or doing things
behind the scenes. I suppose some people might actually like this, but
every time I see someone confused by what''s going on in VB.NET, I wonder
whether they would have had the same confusion had VB.NET not been
invoking some hidden behavior on the programmer''s behalf (another recent
example that comes to mind is VB.NET instantiating a Form-derived class
instance as some sort of "default" instance to use, conflicting with a
previously-created one).

Pete


这篇关于在C#和VB .NET中注册事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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