Subject: ". net Q & A Activities"-questions asked by many people-C # delegation and events Author: TomMax) Reputation: 100 Forum:. NET technology C # Problem count: 20 Replies: 62 Posting time: 07:52:33 Many people ask about delegation and event handling in. Net. Here is a simple example: For example, in a company (scenario), you are the boss and have two employees, Xiao Zhang and Xiao Wang. You ordered Mr. Zhang to play the game. If Mr. Zhang played the game, he deducted 500 yuan from Mr. Zhang. This is the delegate in reality. In fact, in writing a program, the programmer is the boss, and Xiao Zhang and Xiao Wang are two objects. Playing a game is a method. Another game event is played by John. Mr. Smith is the object of event processing. He is responsible for deducting Mr. Zhang's money by 500. Therefore, delegation has the following elements: 1. The object that inspires the event is Xiao Zhang. 2. The object for processing the object event is John. 3. Define the delegate, that is, let Mr. Smith monitor Mr. Zhang. If all three elements are met, you can write a complete event. The following example shows that the application is successfully edited and run in the vs. net2003 C # Console: using System;
Namespace CSharpConsole { // Define the delegate Handler Public delegate void PlayGameHandler (object sender, System. EventArgs e );
Public class scenario { [STAThread] Public static void Main (string [] args) { Console. WriteLine ("scenario started ."); // Generate Tom Wang w = new Wang (); // Generate a small account John z = new John ();
// Specify monitoring Z. PlayGame + = new PlayGameHandler (w. Deduction );
// Start playing the game Z. Play the game ();
Console. WriteLine ("End of scenario "); Console. ReadLine (); } }
// The person responsible for fee deduction Public class Wang { Public Wang () { Console. WriteLine ("generate Tom "); }
Public void deduction (object sender, EventArgs e) { Console. WriteLine ("Mr. Wang: Good boy, dare to play games during work hours "); Console. WriteLine ("Mr. Wang: How much does your kid have "); John f = (John) sender; Console. WriteLine ("John's money:" + f. Qian. ToString ()); Console. WriteLine ("start to deduct money "); System. Threading. Thread. Sleep (500 ); F. Money = f. Money-500; Console. WriteLine ("the button is finished. Now James is still there:" + f. Qian. ToString ()); } }
// If the game is played, an event is triggered. Public class Xiao Zhang { // Define an event first, which means "Xiao Zhang" is playing the game. Public event PlayGameHandler PlayGame; // Save the small money variable Private int m_Money;
Public Xiaozhang () { Console. WriteLine ("generating images ."); M_Money = 1000; // constructor, which initializes the small Zhang's money. }
Public int money // This property can be used to operate on the small Zhang's money. { Get { Return m_Money; } Set { M_Money = value; } }
Public void () { Console. WriteLine ("John started playing the game .."); Console. WriteLine ("Xiao Zhang: CS fun, hahaha! I play .."); System. Threading. Thread. Sleep (500 ); System. EventArgs e = new EventArgs (); OnPlayGame (e ); }
Protected virtual void OnPlayGame (EventArgs e) { If (PlayGame! = Null) { PlayGame (this, e ); } } }
} |