[英]VB.NET to C# with Event Handlers
Hi all sorry but you must get this all the time, I have tried to search for it but could not find anything specific to what I am looking for. 大家好,抱歉,您必须一直得到这个,我已经尝试搜索它,但是找不到我想要的东西。
Basically I pretty much know VB.NET fully and I use it constantly, something I want to get into is C# so I have decided to use my free time to try and get a grip on this. 基本上,我几乎完全了解VB.NET,并且我经常使用它,所以我想使用C#,所以我决定利用自己的空闲时间来尝试一下。 I am doing this by using one of my old VB.NET custom binary objects and converting it by hand (not really using a converter as I want to learn it rather than just converting it.) I am however using the internet to guide me.
我是通过使用一个旧的VB.NET自定义二进制对象并手动进行转换来实现此目的的(并不是我想学习它而是真正地使用转换器,而不仅仅是转换它。)但是,我使用互联网来指导我。 So far I am trying to create custom even handlers my previous VB.NET code was as follows;
到目前为止,我试图创建自定义的偶数处理程序,而我之前的VB.NET代码如下:
Public Event BeforeExecution_Handler()
but doing it in C# seems to be a bit more trickier and i have made the following 但是用C#进行操作似乎有些棘手,我做了以下工作
public event BeforeExecution_HandlerEvent BeforeExecution_Handler;
public delegate void BeforeExecution_HandlerEvent();
No first is this correct, but secondly what is going on here, why do I have to create definitions for it twice. 首先,这是正确的,但其次,这是怎么回事,为什么我必须为其创建两次定义。 I am having a guess that the delegate section is where you put variables but why is it like this.
我猜测委托部分是您放置变量的地方,但为什么会这样呢? Can somebody explain
有人可以解释吗
In VB.NET you can implicitly create a delegate, so you can just do something like this: 在VB.NET中,您可以隐式创建一个委托,因此您可以执行以下操作:
Declaration: 宣言:
Public Event MsgArrivedEvent(ByVal message As String) 'behind the scenes a delegate is created for you
Invocation: 调用:
RaiseEvent MsgArrived("foo")
In C# you have to use delegates. 在C#中,您必须使用委托。
Declaration: 宣言:
public delegate void MsgArrivedEventHandler(string message);
public event MsgArrivedEventHandler MsgArrivedEvent;
Invocation: 调用:
MsgArrivedEvent("Test message");
Note that you can also explicitly create a delegate in VB.NET in the same way as C# but this is just more typing for no gain really: 请注意,您还可以使用与C#相同的方式在VB.NET中显式创建一个委托,但这实际上只是增加了一点输入而已:
Declaration: 宣言:
Public Delegate Sub MsgArrivedEventHandler(ByVal message As String)
Public Event MsgArrivedEvent As MsgArrivedEventHandler
Invocation: 调用:
RaiseEvent MsgArrivedEvent("foo")
Also note that best practise is actually to use use a sender
and EventArgs
class (or a class inherited from it) as the parameters to the Event/Delegate: 另请注意,最佳实践实际上是使用
sender
和EventArgs
类(或从其继承的类)作为Event / Delegate的参数:
public delegate void MsgArrivedEventHandler(object sender, EventArgs e);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.