473,811 Members | 2,896 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 2816
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.co m

"Elliot" <el************ @hotmail.co.ukw rote in message
news:39******** *************** ***********@mic rosoft.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(stri ng value) {
//...
}

Marc
Jul 1 '08 #3
Works, thank you.
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c omwrote in
message news:uu******** ******@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]
- mv*@spam.guard. caspershouse.co m

"Elliot" <el************ @hotmail.co.ukw rote in message
news:39******** *************** ***********@mic rosoft.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**********@g mail.comwrote in message
news:uV******** ******@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
Jul 2 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1444
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 on a different set of types then the range. I currently have worked out a way to do it (see code below) but it seems fairly involved and I was wondering if there is a better way of doing it?
3
3073
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; document.forms.Button1.onclick = EventHandler;
6
3118
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
4425
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 button2. For Example, 1. Click button1 2. Click button2
3
3479
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 InitCombo() {
4
5485
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 dropdownlist to the screen for selection. This continues until there are no children, and then it checks for a help article list based on that last selection and displays actual articles for display. Adding the controls and getting everything...
3
1563
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, how can I write a function that meets the signature for event E, when the compiler doesn't know about A ? I can solve this problem using Delegate.CreateDelegate
3
2786
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 *************************/ #include <boost/shared_ptr.hpp> #include <list> class T{ // simplified this class to the simplest form public: T(int t):capacity(t){}
2
1955
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 argument? since we are passing second parameter as pointer to socket address structure, so it can calculate itself the length of the structure. Thanks, Manjunath
0
9734
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10401
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10416
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10138
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7676
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6897
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5704
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4357
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3881
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.