By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,895 Members | 1,106 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,895 IT Pros & Developers. It's quick & easy.

Third argument for EventHandler

P: n/a
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)
{
}

Jul 1 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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]
- mv*@spam.guard.caspershouse.com

"Elliot" <el************@hotmail.co.ukwrote in message
news:39**********************************@microsof t.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)
{
}

Jul 1 '08 #2

P: n/a
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(string value) {
//...
}

Marc
Jul 1 '08 #3

P: n/a
Works, thank you.
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:uu**************@TK2MSFTNGP06.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]
- mv*@spam.guard.caspershouse.com

"Elliot" <el************@hotmail.co.ukwrote in message
news:39**********************************@microsof t.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)
{
}

Jul 2 '08 #4

P: n/a
Thank you too.
"Marc Gravell" <ma**********@gmail.comwrote in message
news:uV**************@TK2MSFTNGP04.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(string value) {
//...
}

Marc
Jul 2 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.