• Saves Valuable Time
  • Trusted Accuracy for 12 Years
  • 15-Day Money-Back Guarantee

C# Equivalent to VB Events

Event Declarations

VB.NET C#
Public Event EventOne(ByVal p As Foo) 'implicit delegate

Public Event EventTwo As FooDelegate 'explicit delegate
public delegate void EventOneEventHandler(Foo p);
public event EventOneEventHandler EventOne;

public event FooDelegate EventTwo;

Event Wireups

VB.NET C#
AddHandler ControlA.Click, New ControlA.SomeEventHandler(AddressOf ControlA_Click)

AddHandler ControlA.Click, AddressOf ControlA_Click
ControlA.Click += new ControlA.SomeEventHandler(ControlA_Click);

ControlA.Click += ControlA_Click;

Strict Conversion of WithEvents/Handles

VB.NET C#
Class TestClass
    Private WithEvents MyField As FooDelegate

    Sub Method() Handles MyField.FooEvent
    End Sub
End Class
using System.Runtime.CompilerServices;
using System.Diagnostics;

class TestClass
{
    [AccessedThroughProperty(nameof(MyField))]
    private FooDelegate _MyField;
    private FooDelegate MyField
    {
        [DebuggerNonUserCode]
        get
        {
            return _MyField;
        }
        [MethodImpl(MethodImplOptions.Synchronized), DebuggerNonUserCode]
        set
        {
            if (_MyField != null)
                _MyField.FooEvent -= Method;

            _MyField = value;

            if (value != null)
                _MyField.FooEvent += Method;
        }
    }

    void Method()
    {
    }
}

Casual Conversion of WithEvents/Handles

VB.NET C#
Class TestClass
    Private WithEvents MyField As FooDelegate

    Sub Method() Handles MyField.FooEvent
    End Sub
End Class
class TestClass
{
    private FooDelegate MyField;

    void Method()
    {
    }

    public TestClass()
    {
        SubscribeToEvents();
    }

    private bool EventsSubscribed = false;
    private void SubscribeToEvents()
    {
        if (EventsSubscribed)
            return;
        else
            EventsSubscribed = true;

        MyField.FooEvent += Method;
    }
}


Use Instant C# to convert from VB.NET to C#.     

Additional resource: VB.NET and C# Equivalents


Copyright © 1997 – 2017 Tangible Software Solutions Inc.