473,499 Members | 1,609 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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)
{
}

Jul 1 '08 #1
4 2801
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1431
by: velthuijsen | last post by:
The STL sort only accepts a function that is defined in the form of bool Fname(<type>, <type>) in which <type> is the type of range to be sorted. I'm looking for a way to be able to sort the range...
3
3045
by: Remco | last post by:
Hello, Serverside I'm generating a html page. There are different controls for which I want to create an eventhandler manually. Like: document.forms.TextBox1.onchange = EventHandler;...
6
3095
by: thesushant | last post by:
hi, whats the use of third argument to main( ), i.e. environmental parameters.... if i am not wrong ? 1st 1 is argc 2nd is argv and what bout the 3rd 1??????????? sushant
12
4399
by: aplaxas | last post by:
Hi! "CDemo.Call" eventHandler is added to "button3.click" Event when both button1 and button2 are clicked. However, I want to add "cd.Call" only one time even though I clicked both button1 and...
3
3466
by: Michael Conroy | last post by:
Hi... Synposis... Throws exception: "Specified argument was out of the range of valid values." Read on for the juicy tidbits. MySimpleClassCol mscc=new MySimpleClassCol(); private void...
4
5447
by: DotNetJunky | last post by:
I have built a control that runs an on-line help system. Depending on the category you selected via dropdownlist, it goes out and gets the child subcategories, and if there are any, adds a new...
3
1547
by: Stephen Ahn | last post by:
Suppose I have object O with an event E that are defined in an assembly M that I am referencing in my code via reflection. M also defines object A which is an argument to E. At compile-time,...
3
2763
by: zhang.yongpeng | last post by:
Hello, I met some problems when trying to sort a list that has shared_ptr in it. here is the non-compliable code. test.cpp: /********************* begin of code *************************/...
2
1937
by: manjuks | last post by:
Hi All, int bind(int SOCKFD, struct sockaddr *MYADDR, int ADDRLEN); In the above system call, the third argument is the length of socket address structure. My question is why we need third...
0
7014
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7229
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6905
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7395
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5485
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4921
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3108
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3103
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
311
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.