简体   繁体   中英

Confused about VB/C# conversion with event handlers

I've used just about every conversion tool out there to convert a snippet of VB.NET to C#. Aside from some differences in conversion, the one thing that I am having trouble grasping is event handler conversion. Looking at some pure C# code, I see things like this:

protected override void OnPreRender(EventArgs e)
{
    base.OnPreRender(e);
    ...
}

which looking at MSDN , raises the PreRender event. OK. Now, taking some VB.NET code:

Private Sub Page_PreRender(ByVal sender As Object, 
    ByVal e As System.EventArgs) Handles Me.PreRender
....
EndSub

which handles the same PreRender event and translating it with a conversion tool ( DeveloperFusion seemed to give the cleanest conversion of the Event Handler) gave this result:

public class MyClass : UserControl
{
    private void Page_PreRender(object sender, System.EventArgs e)
    {
        ...
    }

    public MyClass()
    {
        PreRender += Page_PreRender;
    }
}

Being new to this, my main question is whether or not the original (or pure) C# code first listed above is basically the same as the converted C# code (other than maybe with the object sender portion)? And if not a brief explanation would be much appreciated.

Essentially, I am just wondering whether the PreRender += Page_PreRender; portion is superfluous.

The first C# code block presentes an override of the base classes' OnPreRender function, while the second is an event handler to the PreRender event.

Yes, they are different, but functionally they are more-or-less equivialent, because as you mentioned the base classes OnPreRender function will fire the PreRender event.

Essentially it represents two ways to accomplish the same thing, and the mechanism you choose is mostly a question of semantics. I say mostly because there may exist explicit reasons to choose one method over the other, but if you aren't aware of any, just choose one.

I prefer the override myself, as you typically use events to broadcast notifications to external listeners rather than to derived instances of an object.

The PreRender += Page_PreRender is not superfluous in your example, as it is required to hookup the event handler when you are using that mechanism. The VB version you posted is implemented using events rather that an override, so the conversion is correct. The "wire-up" for these events are implicit in VB.net, so you won't find an equivalent statement in the VB version.

Is the PreRender += Page_PreRender; portion superfluous?

No - that code is C#'s way of assigning the event handler, which is done in VB with the Handles Me.PreRender clause.

Being a n00b, my main question is whether or not the original (or pure) C# code first listed above is basically the same as the converted C# code.

Yes, the conversion tool is right !


Explanation

VB.NET's Handles is the equivalent to PreRender += Page_PreRender; .

Handles basically does the exact same thing in the background; adding an eventhandler.

So Codesnippet #1 and Codesnippet #2 are the same, except that you need to manage the handler by yourself in #2 (Like removing the handler if the Windows Form gets disposed).

Codesnippet #1 (VB.NET)

Private Sub Page_PreRender(ByVal sender As Object, 
    ByVal e As System.EventArgs) Handles Me.PreRender
....
End Sub

Codesnippet #2 (VB.NET)

AddHandler Me.PreRender, AddressOf Page_PreRender

So obviously, no converted code is superfluous.

The conversion was done correctly, but it is not equivalent to the original C#:

which looking at MSDN, raises the PreRender event.

The original code is not a handler, it's an invoker. OnPreRender is called to raise PreRender . If the VB code raised PreRender in Page_PreRender , it would lead to infinite recursion.

The overridable "On[event name]" pattern is used to allow subclasses to "handle" events, but it's not the same as a handler. The call to base / MyBase is what actually raises the event, so essentially by overriding one of these methods you're forcing an action to occur completely before or after (depending on where you call the base) all event handlers are invoked.

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