简体   繁体   中英

C# to VB.NET Event Conversion

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.

This is the original C# code

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

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.

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

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())

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM