Search code examples
c#vb.neteventshandles

Migrating Handles (HttpApplication) methods from VB.NET to C#?


I'm migrating some code from VB.NET to C# (4.0).

I find structurs like:

Private Sub WhitePointHttpApplicationBase_BeginRequest(sender As Object, e As System.EventArgs) Handles Me.BeginRequest

End Sub

What is the most straight-forward to translate such behavior in C#?


Solution

  • In the constructor add this.BeginRequest+=WhitePointHttpApplicationBase_BeginRequest;

    You'll also need the method to exist: private void WhitePointHttpApplicationBase_BeginRequest(sender As Object, e As System.EventArgs) { //Your event code here }

    The following is your code from the comment with corrections:

    namespace WhitePoint.Solutions.Web 
    { 
        public abstract class WhitePointHttpApplicationBase : HttpApplication { 
    
            protected WhitePointHttpApplicationBase()
            {
                this.BeginRequest += WhitePointHttpApplicationBase_BeginRequest; 
            }
            #region "Member" 
            #endregion 
    
            private void WhitePointHttpApplicationBase_BeginRequest(object sender, EventArgs e) { } 
        } 
    } 
    

    The this.BeginRequest += was not in a constructor.

    The abstract class now how a default protected constructor, any classes that inherit should call this base constructor if you expect the code to run.