One or two good examples of delegated events!

Source: Internet
Author: User

Both examples use real examples to explain the process of delegate event processing. Good!

Example 1

Public Delegate void eateventhandler (Object sender, eateventargs E );
This can also be avoided. A parameter can be defined directly.
Public Delegate void eateventhandler (string E); however, the parameters should be relatively good when used.

Using system;

Namespace nseventsample
{
/// <Summary>
/// Class eateventargs must inherit from class eventargs to encapsulate data when an event is triggered.
/// </Summary>
Public class eateventargs: eventargs
{
Public String restrauntname; // hotel name
Public decimal moneyout; // prepare the consumption amount
}

/// <Summary>
/// This delegate is used to describe the method header (mode) for processing meal events)
/// </Summary>
Public Delegate void eateventhandler (Object sender, eateventargs E );

/// <Summary>
/// The class master that triggers the eateevent. This class must
/// 1. Declare an event named eatevent: public event eateventhandler eatevent;
/// 2. Use the oneatevent method to trigger a meal event and transmit data to the methods that process the event;
/// 3. What is the cause of an event in some circumstances? When you are hungry. Use the hungrg Method for simulation.
/// </Summary>
Public class master
{
// Declare the event
Public event eateventhandler eatevent;

// Method for triggering an event
Public void oneatevent (eateventargs e)
{< br> If (eatevent! = NULL)
{< br> eatevent (this, e);
}< BR >}

// When the master is hungry, he will specify the place to eat and the amount of consumption.
Public void hungry (string restrauntname, decimal moneyout)
{
Eateventargs E = new eateventargs ();
E. restrauntname = restrauntname;
E. moneyout = moneyout;

Console. writeline ("said the Host :");
Console. writeline ("I'm hungry, I want to go to {0} for dinner, consume {1} RMB", E. restrauntname, E. moneyout );

// Events
Oneatevent (E );
}
}

/// <Summary>
/// Class servant (Servant) has a method arrangefood (arrange food) to handle the host's meal event
/// </Summary>
Public class servant
{
Public void arrangefood (Object sender, eateventargs E)
{
Console. writeline ();
Console. writeline ("said the servant :");
Console. writeline ("my master, your command is :");
Console. writeline ("meal location -- {0}", E. restrauntname );
Console. writeline ("prepare for consumption -- {0} RMB", E. moneyout );
Console. writeline ("OK, we are arranging for you ........ ");
Console. writeline ("Master, your food is here, please use it slowly ");
}
}

///


// class God: the servant of Qinshihuang (Qin Shihuang) is Lisi (Li Si ), and asked Lee's arrangefood
// method to handle the Qinshihuang dinner event: Qinshihuang. eatevent + = new eateventhandler (Lishi. arrangefood);
//
public class God
{< br> Public static void main ()
{< br> master Qinshihuang = new master ();
servant Lishi = new servant ();

Qinshihuang. eatevent + = new eateventhandler (Lishi. arrangefood );

// Qin Shihuang is hungry. He wants to go to the Hilton Hotel and spend 5000 yuan
Qinshihuang. Hungry ("Hilton Hotel", 5000.0 m );
}
}
}
 

The second example is from:
Http://community.csdn.net/Expert/topic/2651/2651579.xml? Temp =. 7183191.

For example, in a company (scenario), you are the boss and have two employees, Xiao Zhang and Xiao Li. You command Xiao Zhang to pay attention to Xiao Li. If you play games online during software development, you will record it and deduct 100 yuan from Xiao Li's salary. This is actually a delegate in reality.

Now we provideCode, C # ConsoleProgram, Compiled and run through
Using system;

Namespace csharpconsole
{
Public class scenario
{
[Stathread]
Public static void main (string [] ARGs)
{
Console. writeline ("the scenario has 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 ("JOHN: look at how much your kid has ...");
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 );
}
}
}

// Define the delegate Handler
Public Delegate void playgamehandler (Object sender, system. eventargs E );
}

we also recommend a series of articles :
http://blog.joycode.com/percyboy/archive/2005/01/22/43438.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.