//---assign a delegate to del of c1---
c1.del = new Class1.Class1Delegate(d1);
}
static private void DoSomething() {
//...
}
To use Class2
, you create an instance of Class2
and then assign a delegate to the evt
event using the +=
operator:
static void Main(string[] args) {
//...
//---create a delegate---
Class2.Class2Delegate e2 =
new Class2.Class2Delegate(DoSomething);
Class2 c2 = new Class2();
//---assign a delegate to evt of c2---
c2.evt += new Class2.Class2Delegate(d1);
}
If you try to use the =
operator to assign a delegate to the evt
event, you will get a compilation error:
c2.evt = new Class2.Class2Delegate(d1); //---error---
This important restriction of event is important because defining a delegate as an event will ensure that if multiple clients are subscribed to an event, another client will not be able to set the delegate to null (or simply set it to another delegate). If the client succeeds in doing so, all the other delegates set by other client will be lost. Hence, a delegate defined as an event can only be set with the +=
operator.
Passing State Information to an Event Handler
In the preceding program, you simply raise an event in the AlarmClock
class; there is no passing of information from the class back to the event handler. To pass information from an event back to an event handler, you need to implement your own class that derives from the EventArgs
base class.
In this section, you modify the previous program so that when the set time is up, the event passes a message back to the event handler. The message is set when you instantiate the AlarmClock
class.
First, define the AlarmClockEventArgs
class that will allow the event to pass back a string to the event handler. This class must derive from the EventArgs
base class:
public class AlarmClockEventArgs : EventArgs {
public AlarmClockEventArgs(string Message) {
this.Message = Message;
}
public string Message { get; set; }
}
Next, define a delegate called AlarmClockEventHandler
with the following signature:
public delegate void AlarmClockEventHandler(object sender, AlarmClockEventArgs e);
Replace the original TimesUp
event statement with the following statement, which uses the AlarmClockEventHandler
class:
//---public event EventHandler TimesUp;---
public event AlarmClockEventHandler TimesUp;
Add a Message
property to the class so that users of this class can set a message that will be returned by the event when the time is up:
public string Message { get; set; }
Modify the onTimesUp
virtual method by changing its parameter type to the new AlarmClockEventArgs
class:
protected virtual void onTimesUp(AlarmClockEventArgs e) {
if (TimesUp != null) TimesUp(this, e);
}
Finally, modify the t_Elapsed
event handler so that when you now call the onTimesUp()
method, you pass in an instance of the AlarmClockEventArgs
class containing the message you want to pass back to the event handler:
void t_Elapsed(object sender, ElapsedEventArgs e) {
if (DateTime.Now >= this.AlarmTime) {
onTimesUp(new AlarmClockEventArgs(this.Message));
t.Stop();
}
}
Here's the complete program: using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
public class AlarmClockEventArgs : EventArgs {
public AlarmClockEventArgs(string Message) {
this.Message = Message;
}
public string Message { get; set; }
}
public delegate void AlarmClockEventHandler(object sender, AlarmClockEventArgs e);
class AlarmClock {
Timer t;
public event AlarmClockEventHandler TimesUp;
protected virtual void onTimesUp(AlarmClockEventArgs e) {
if (TimesUp != null) TimesUp(this, e);
}
public DateTime AlarmTime { get; set; }
public string Message { get; set; }
public AlarmClock() {
t = new Timer(100);
t.Elapsed += new ElapsedEventHandler(t_Elapsed);
}
public void Start() {
t.Start();
}
void t_Elapsed(object sender, ElapsedEventArgs e) {
if (DateTime.Now >= this.AlarmTime) {
onTimesUp(new AlarmClockEventArgs(this.Message));
t.Stop();
}
}
}
With the modified AlarmClock
class, your program will now look like this:
namespace Events {
class Program {
static void c_TimesUp(object sender, AlarmClockEventArgs e) {
Console.WriteLine(DateTime.Now.ToShortTimeString() + ": " + e.Message);
}
static void Main(string[] args) {
AlarmClock c = new AlarmClock() {
//---alarm to sound off at 16 May 08, 9.50am---
AlarmTime = new DateTime(2008, 5, 16, 09, 50, 0, 0),
Message = "Meeting with customer."
};
c.TimesUp += new AlarmClockEventHandler(c_TimesUp);
c.Start();
Console.ReadLine();
}
}
}
Figure 7-10 shows the output when the AlarmClock
fires the TimesUp
event.
Figure 7-10
This chapter discussed what delegates are and how you can use them to invoke other functions, as well as how you can use delegates to implement callbacks so that your application is more efficient and responsive. One direct application of delegates is events, which make GUI operating systems such as Windows possible. One important difference between delegates and events is that you cannot assign a delegate to an event by using the =
operator.
Chapter 8
Strings and Regular Expressions
One of the most common data types used in programming is the string. In C#, a string is a group of one or more characters declared using the string
keyword. Strings play an important part in programming and are an integral part of our lives — our names, addresses, company names, email addresses, web site URLs, flight numbers, and so forth are all made up of strings. To help manipulate those strings and pattern matching, you use regular expressions, sequences of characters that define the patterns of a string. In this chapter, then, you will:
Читать дальше