C#到VB.NET事件转换

时间:2022-03-09 19:00:30

Porting an application from C# (1.1 framework) to VB.NET (3.5 framework), and I have this one last event based issue I cannot get my head around.

将应用程序从C#(1.1框架)移植到VB.NET(3.5框架),我有这个最后一个基于事件的问题,我无法理解。

This is the original C# code

这是原始的C#代码

public delegate void SpecialEventHandler(object sender,SpecialEventArgs e);
public event SpecialEventHandler SpecialEvent = null;

_SpecialLogWriter  SpecialWriter = new _SpecialLogWriter(this.SpecialEvent);

This is the converted VB.NET code

这是转换后的VB.NET代码

Public Delegate Sub SpecialEventHandler(ByVal sender as Object, ByVal e as SpecialEventArgs)
Public Event SpecialEvent as SpecialEventHandler

Dim SpecialWriter as New _SpecialLogWriter(Me.SpecialEvent)

The SpecialLogWriter constructor is expecting a SpecialEventHandler, but the Me.SpecialEvent in the constructor of SpecialLogWriter gives me the error message that this is an event and cannot be called directly.

SpecialLogWriter构造函数需要一个SpecialEventHandler,但是SpecialLogWriter构造函数中的Me.SpecialEvent会给出错误消息,指出这是一个事件,不能直接调用。

Am I missing another delegate, or is this just one of this declaration issues between the languages?

我错过了另一位代表,或者这只是这些语言之间的声明问题之一?

2 个解决方案

#1


You say the constructor expects event handler but you're passing an event, not event handler. Shouldn't you use:

你说构造函数需要事件处理程序但你传递的是事件,而不是事件处理程序。你不应该使用:

Dim SpecialWriter as New _SpecialLogWriter(New SpecialEventArgs())

#2


I am forced to wonder if the original code worked properly to begin with. I simply removed the event tag from the SpecialEvent declaration, and all seems to be working at this point.

我不得不怀疑原始代码是否正常工作。我只是从SpecialEvent声明中删除了事件标记,所有这些似乎都在这一点上起作用。

#1


You say the constructor expects event handler but you're passing an event, not event handler. Shouldn't you use:

你说构造函数需要事件处理程序但你传递的是事件,而不是事件处理程序。你不应该使用:

Dim SpecialWriter as New _SpecialLogWriter(New SpecialEventArgs())

#2


I am forced to wonder if the original code worked properly to begin with. I simply removed the event tag from the SpecialEvent declaration, and all seems to be working at this point.

我不得不怀疑原始代码是否正常工作。我只是从SpecialEvent声明中删除了事件标记,所有这些似乎都在这一点上起作用。