Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 22 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
22
Dung lượng
640,85 KB
Nội dung
Delegates and Events Chapter 12 Classes are reference types that allow you to create instances of objects and use them in special ways to meet your application’s requirement. Another reference type in C# is delegate. Delegates allow you to change the reference to a method at runtime. This means that you can decide the execution of a method at run-time, based on the requirements of your application. The method can be activated at the occurrence of an event, where an event is associated with the delegate to call the method at run-time. This chapter discusses the creation of delegates. This chapter also discusses how to implement multicast delegates. In addition, it discusses how you can use events with delegates. In this chapter, you will learn to: Implement delegates Implement multicast delegates Use events with delegates Objectives ¤NIIT Delegates and Events 12.3 Delegates in C# allow you to dynamically change the reference to the methods in a class. Consider an example of a coffee vending machine, which dispenses different flavors of coffee, such as cappuccino and black coffee. On selecting the desired flavor of coffee, the vending machine decides to dispense the ingredients, such as milk powder, coffee powder, hot water, cappuccino coffee powder. All the materials are placed in different containers inside the vending machine. The required material is dispensed when you select a flavor. Suppose, you select black coffee, the vending machine will call methods to dispense hot water and coffee powder only. The reference to these methods is made dynamically, when you press the required button to dispense black coffee. A delegate is a reference type variable, which holds the reference to a method. This reference can be changed at runtime, as desired. Although, delegates are a general-purpose mechanism for indirectly calling methods at runtime, their primary use in C# programming is for implementing events and the call-back methods. To implement delegates in your application you need to declare delegates, instantiate delegates and use delegates. The methods that can be referenced by a delegate are determined by the delegate declaration. The delegate can refer to the methods, which have the same signature as that of the delegate. Consider the following example of delegate declaration: public delegate void MyDelegate (string s); In the preceding example, the declared delegate type can be used to reference any method. This method should have a single parameter of string type and it does not return any value. The following syntax is used for delegate declaration: delegate <return type><delegate-name><parameter list> Introducing Delegates Declaring Delegates 12.4 Delegates and Events ¤NIIT Create the delegate object of the delegate type, which you have already created. Assign the address of the required method to the delegate object. This can be done by calling the constructor of the delegate class and passing the method name. The following example shows how to assign the address of a method to a delegate variable: public void DelegateFunction(string PassValue) { // Method implementation Here } //Delegate Declaration public delegate void MyDelegate(string ArgValue); public void UseMethod() { //Delegate Instantiation MyDelegate DelegateObject = new MyDelegate(DelegateFunction); } In the preceding example, the signature and return type of DelegateFunction matches with the delegate declaration of the MyDelegate delegate. the MyDelegate delegate can hold the address of DelegateFunction. DelegateObject is a delegate object of the type, MyDelegate. The address of the DelegateFunction is assigned to the DelegateObject by passing, the name of the function to the delegate constructor. You can call the delegate by giving the name of the delegate and by passing parameters, if required. Using delegates is similar to calling methods. Consider a situation where you need to print information to a file and a screen. There is some common information that needs to go to the file and to the screen. There is also some specific information for both. The methods to print the information to the file and screen are different. You can call these methods at runtime by passing the common information. The following example shows how to use a delegate: // This code is to print data to the output device, which is either a file or a screen using System; using System.IO; // Program to write the data to the console and file namespace Chapter12_Ex1 { public class PrintToDevice { Instantiating Delegates Using Delegate ¤NIIT Delegates and Events 12.5 //Creating the variables of Stream classes static FileStream FStream; static StreamWriter SWriter; //Defining a Delegate public delegate void PrintData(String s); //Method to print a string to the console public static void WriteConsole (string str) { Console.WriteLine("{0} Console",str); } //Method to print a string to a file public static void WriteFile (string s) { //Initializing stream objects FStream = new FileStream("c:\\StoreData.txt", FileMode.Append, FileAccess.Write); SWriter = new StreamWriter(FStream); s= s + " File"; //Writing a string to the file SWriter.WriteLine(s); //removing the content from the buffer SWriter.Flush(); SWriter.Close(); FStream.Close(); } //Method to send the string data to respective methods public static void DisplayData(PrintData PMethod) { PMethod("This should go to the"); } public static void Main() { //Initializing the Delegate object PrintData Cn = new PrintData (WriteConsole); PrintData Fl = new PrintData (WriteFile); //Invoking the DisplayData method with the Delegate object as the argument //Using Delegate DisplayData (Cn); DisplayData (Fl); Console.ReadLine(); } } } In the preceding example, the WriteConsole() and WriteFile() methods are used to write the information to the screen and to the file. The delegate variable PrintData is used to refer to the WriteConsole() and WriteFile() methods. Delegates are of two types and depending upon the requirement of the application the suitable type of delegate is selected. 12.6 Delegates and Events ¤NIIT There are two types of delegates, Single-cast delegate and Multicast delegate. A Single-cast delegate can call only one method at a time, whereas a Multicast delegate can call multiple methods at the same time. Single-Cast Delegate A single-cast delegate derives from the System.Delegate class. It contains reference to one method only at a time. In the preceding example, the delegate used is a single-cast delegate. The WriteConsole() and WriteFile() methods are being referenced by the delegate, PrintData, one after the other, at runtime. Consider the coffee vending machine example. To provide black coffee a delegate that holds the reference to the methods to dispense hot water and coffee powder is used. The reference to these methods is made dynamically, but one after the other. This means coffee powder and hot water will be dispensed from the machine serially. Multicast Delegate A multicast delegate derives from the System.MulticastDelegate class. It contains an invocation list of multiple methods. In multicasting you create a single delegate that invokes multiple encapsulated methods. You need to ensure the return type of all these delegates is same. Consider the coffee vending machine example. You are dispensing black coffee, which in turn calls the methods to dispense hot water and coffee powder. If you want the methods to dispense hot water and coffee powder to be called at the same time, you can make use of a multicast delegate. Multicast delegates hold the reference of more than one method therefore, if you call a multicast delegate it will execute all the methods it wraps in the calling order. The multiple methods called by the delegate in this case should not return any value because several multicast delegates are called consecutively and you cannot wait to get the return value from each of these methods. Consider the preceding example of printing a message to a file and to a screen in which the WriteFile() and WriteConsole() methods are called by using a single-cast delegate. This example considers a situation where all the methods are called at the same instance. Using the same delegate, multicasting helps to call both the WriteFile() method and the WriteConsole() method in one call. The following code shows how to use a multicast delegate: Types of Delegates ¤NIIT Delegates and Events 12.7 using System; using System.IO; // Program to write the data to the console and file namespace Chapter12_Ex2 { public class PrintToDevice { //Creating the variables of Stream classes static FileStream FStream; static StreamWriter SWriter; //Defining a Delegate public delegate void PrintData(String s); //Method to print a string to the console public static void WriteConsole (String str) { Console.WriteLine("{0} Console",str); } //Method to print a string to a file public static void WriteFile (String s) { //Initializing stream objects FStream = new FileStream("c:\\StoreData.txt", FileMode.Append, FileAccess.Write); SWriter = new StreamWriter(FStream); s= s + " File"; //Writing a string to the file SWriter.WriteLine(s); //removing the content from the buffer SWriter.Flush(); SWriter.Close(); FStream.Close(); } //Method to send the string data to respective methods public static void DisplayData(PrintData PMethod) { PMethod("This should go to the"); } public static void Main() { //Initializing the Delegate object PrintData MlDelegate = new PrintData(WriteConsole); MlDelegate += new PrintData(WriteFile); DisplayData(MlDelegate); MlDelegate -= new PrintData(WriteFile); DisplayData(MlDelegate); } } } 12.8 Delegates and Events ¤NIIT Just a minute: In the preceding example, the multicast delegate, MlDelegate holds reference of both the WriteConsole() and WriteFile() methods. State whether the following statement is True or False: Multicast delegates inherit from the System.Delegate.MulticastDelegate class. Answer: False ¤NIIT Delegates and Events 12.9 < An event is an action or occurrence, such as clicks, key presses, mouse movements, or system generated notifications. Applications can respond to events when they occur. An example of a notification is interrupts. Events are messages sent by the object to indicate the occurrence of the event. Events are an effective mean of inter-process communication. They are useful for an object because they provide signal state changes, which may be valuable to a client of the object. Consider an example of an event and the response to the event. A clock is an object that shows 6 AM time and generates an event in the form of an alarm. You accept the alarm event and act accordingly. The following figure shows the alarm event and handling of the event. Alarm Event and its Handling Working with Events 12.10 Delegates and Events ¤NIIT The following figure is the generalized representation that explains events and event handling. Events and Event Handling In C#, delegates are used with events to implement event handling. The .NET Framework event model uses delegates to bind event notifications with methods known as event handlers. When an event is generated, the delegate calls the associated event handler. The events are declared and raised in a class and associated with the event handlers using delegates within the same class or other classes. Events are part of a class and the same class is used to publish its events. The other classes can, however, accept these events or in other words can subscribe to these events. Events use the publisher and subscriber model. A publisher is an object that contains the definition of the event and the delegate. The association of the event with the delegate is also specified in the publisher class. The object of the publisher class invokes the event, which is notified to the other objects. Using Delegates with Events [...]... Debugging or press F5 to execute the application 3 Verify the output of the application NIIT Delegates and Events 12.17 The following window verifies the output of the executed program Output of the Attendance Log Application 12.18 Delegates and Events NIIT , Practice Questions 1 Identify the correct statement about delegates a Before you declare an event inside a class, you can optionally declare a delegate... event and the delegate 4 Match the following delegate concepts with there implementation Delegate Implementation Declaring Delegates public delegate void TestDelegate(string arg); Using Delegates NIIT TestDelegate Var=new TestDelegate(Func); Instantiating Delegates 5 Syntax TestDelegate( “Test”); State whether the given statement is true or false Delegates used with the events should be declared void Delegates. .. derives from the System.Delegate class and Multicast delegate derives from the System.MulticastDelegate Events are the messages sent by an object to indicate the occurrence of an event Events use the publisher and subscriber model A publisher is an object that maintains its internal state A subscriber is an object that registers an interest in an event 12.20 Delegates and Events NIIT ,,, Exercises Exercise... the event class that will pass the value of hour, minute and second The class declares three accessor methods: GetHour, GetMinute, and GetSecond NIIT Delegates and Events 12.13 , Activity: Attendance Log Problem Statement In an air-conditioner manufacturing company, the working time is from 9 AM to 6 PM The management of the company is flexible and it allows workers to arrive to work late by 1 hour The... wants to accept the event and provide a handler to the event The delegate of the publisher class invokes the method of the subscriber class This method in the subscriber class is the event handler The publisher and subscriber model implementation can be defined by the same class The following figure shows the mechanism used by the publisher and subscriber objects Publisher and Subscriber Objects The... class RecordAttendance { 12.16 Delegates and Events NIIT static void Logger(string LogInfo) { Console.WriteLine(LogInfo); } static void Main(string[] args) { AttendanceLogger FileLog = new AttendanceLogger("c:\\process.log"); DelegateEvent DEvent = new DelegateEvent(); // Subscribe the Functions Logger and FileLog.Logger DEvent.EventLog += new DelegateEvent.AttendanceLogHandler(Logger); DEvent.EventLog... which invokes the TimeToRise delegate when it is raised: public delegate void TimeToRise(); private event TimeToRise RingAlarm; NIIT Delegates and Events 12.11 Subscribing to an Event The event of the publisher class needs to be associated with its handler The event handler method is associated with the event using the delegate When the publisher object raises the event, the subscribing object associates... Subscriber Objects The implementation of an event includes events definition, events subscription and events notification Defining an Event The definition of the event in a publisher class includes the declaration of the delegate as well as the declaration of the event based on the delegate The following code defines a delegate named TimeToRise and an event named RingAlarm, which invokes the TimeToRise... the given statement is true or false Delegates used with the events should be declared void Delegates and Events 12.19 Summary In this chapter, you learned that: Delegates allow you to write code that can dynamically change the methods that it calls There are two types of delegates, Single-cast delegate and Multicast delegate A single-cast cast delegate can call only one function Multicast delegate holds... Visual C# from the Project types pane and Console Application from the Templates pane 4 Type the name of the new project as AttendanceApp in the Name text box 12.14 Delegates and Events NIIT 5 Specify the location where the new project is to be created as c:\Chapter12\Activity1 in the Location combo box 6 Click the OK button 7 Open the Solution Explorer window and right-click the Program.cs file The . event and handling of the event. Alarm Event and its Handling Working with Events 12.10 Delegates and Events ¤NIIT The following figure is the generalized representation that explains events and. chapter, you will learn to: Implement delegates Implement multicast delegates Use events with delegates Objectives ¤NIIT Delegates and Events 12.3 Delegates in C# allow you to dynamically. explains events and event handling. Events and Event Handling In C#, delegates are used with events to implement event handling. The .NET Framework event model uses delegates to bind event notifications