0

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#?

John Saunders
  • 160,644
  • 26
  • 247
  • 397

1 Answers1

0

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.

  • I have the following error:WhitePointHttpApplicationBase_BeginRequest is a "Method" but is used like a "type" – Mohammed Qamhawi Feb 17 '13 at 13:17
  • Hopefully the edit will help to clarify what's required. – Jonathon Page Feb 17 '13 at 17:19
  • still I have the same error:this.BeginRequest+=WhitePointHttpApplicationBase_BeginRequest; private void WhitePointHttpApplicationBase_BeginRequest(object sender, EventArgs e) { } – Mohammed Qamhawi Feb 17 '13 at 20:04
  • Without being able to see the context of your code it is difficult to provide you with specific help. If you have put `this.BeginRequest += WhitePointHttpApplicationBase_BeginRequest;` into the constructor of your class, and that class has the method you've shown in your comment everything should work as expected. Provide some further context and I'll help you further. – Jonathon Page Feb 18 '13 at 10:39
  • Full Code:
    namespace WhitePoint.Solutions.Web
    {
        public abstract class WhitePointHttpApplicationBase : HttpApplication
        {
            #region "Member"
           
            #endregion
    
            this.BeginRequest += WhitePointHttpApplicationBase_BeginRequest;
            private void WhitePointHttpApplicationBase_BeginRequest(object sender, EventArgs e)
            {
                
            }
        }
    }
    – Mohammed Qamhawi Feb 18 '13 at 12:46
  • I've edited my answers to show the corrections required to your code. – Jonathon Page Feb 18 '13 at 14:08
  • Thank you so much Jonathon Page. Regards – Mohammed Qamhawi Feb 18 '13 at 15:14