Third argument for EventHandler

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Elliot

    #1

    Third argument for EventHandler

    How can I pass the value of variable text to t except declare text at class
    level?

    ..........
    string text = "text";
    Timer a = new Timer();
    a.Interval = 10000;
    a.Start();
    a.Tick += new EventHandler(t) ;
    ..........

    private void t(object sender, EventArgs eArgs)
    {
    }

  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Third argument for EventHandler

    Elliot,

    Well, the way you have it, you can't, as the EventHandler delegate only
    takes two parameters.

    However, you can make the value text available to t by setting a field
    in the class that t is on, and then use it that way.

    Or, you can use an anonymous method and change t like so:

    private void t(object sender, EventArgs eArgs, string text)
    {

    }

    string text = "text";
    Timer a = new Timer();
    a.Interval = 10000;
    a.Start();
    a.Tick += delegate(object sender, EventArgs e) { t(sender, e, text); };

    If you are using C# 3.0, you can use a lambda expression as well to make
    this code even smaller.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "Elliot" <elliot_barclay @hotmail.co.ukw rote in message
    news:390567A9-2E16-4670-BDDB-5FB759154AEB@mi crosoft.com...
    How can I pass the value of variable text to t except declare text at
    class level?
    >
    ..........
    string text = "text";
    Timer a = new Timer();
    a.Interval = 10000;
    a.Start();
    a.Tick += new EventHandler(t) ;
    ..........
    >
    private void t(object sender, EventArgs eArgs)
    {
    }

    Comment

    • Marc Gravell

      #3
      Re: Third argument for EventHandler

      Since you are *handling* an event, you can't use a custom
      handler/event-args, but you might be able to do what you want via an
      anonymous method:

      a.Tick += delegate { SomeMethod(text );}

      SomeMethod(stri ng value) {
      //...
      }

      Marc

      Comment

      • Elliot

        #4
        Re: Third argument for EventHandler

        Works, thank you.


        "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c omwrote in
        message news:uubWuB52IH A.4536@TK2MSFTN GP06.phx.gbl...
        Elliot,
        >
        Well, the way you have it, you can't, as the EventHandler delegate only
        takes two parameters.
        >
        However, you can make the value text available to t by setting a field
        in the class that t is on, and then use it that way.
        >
        Or, you can use an anonymous method and change t like so:
        >
        private void t(object sender, EventArgs eArgs, string text)
        {
        >
        }
        >
        string text = "text";
        Timer a = new Timer();
        a.Interval = 10000;
        a.Start();
        a.Tick += delegate(object sender, EventArgs e) { t(sender, e, text); };
        >
        If you are using C# 3.0, you can use a lambda expression as well to
        make this code even smaller.
        >
        >
        --
        - Nicholas Paldino [.NET/C# MVP]
        - mvp@spam.guard. caspershouse.co m
        >
        "Elliot" <elliot_barclay @hotmail.co.ukw rote in message
        news:390567A9-2E16-4670-BDDB-5FB759154AEB@mi crosoft.com...
        >How can I pass the value of variable text to t except declare text at
        >class level?
        >>
        > ..........
        > string text = "text";
        > Timer a = new Timer();
        > a.Interval = 10000;
        > a.Start();
        > a.Tick += new EventHandler(t) ;
        > ..........
        >>
        > private void t(object sender, EventArgs eArgs)
        > {
        > }
        >
        >

        Comment

        • Elliot

          #5
          Re: Third argument for EventHandler

          Thank you too.


          "Marc Gravell" <marc.gravell@g mail.comwrote in message
          news:uVWVEF52IH A.2524@TK2MSFTN GP04.phx.gbl...
          Since you are *handling* an event, you can't use a custom
          handler/event-args, but you might be able to do what you want via an
          anonymous method:
          >
          a.Tick += delegate { SomeMethod(text );}
          >
          SomeMethod(stri ng value) {
          //...
          }
          >
          Marc

          Comment

          Working...