Some information will be output in the middle of the program data processing, such as: initialization, end, error, etc., For the previous information, sometimes you have to output different types of information to different directions, and sometimes this is not known to the data operator. When C can be used to solve the function pointer, now you can delegate it in C #, which is the most basic application that commissioned.
Now look at a simple example :)
1) Define the running test class
Public Delegate Void BaseInfoHandler (String Str); // Defines Basic Information Output Delegation
Public Delegate Void ErrorInfoHandler (String Str); // Defines Error Information Output Delegation
Public Class Test
{
Public Event BaseInfoHandler Baseinfoout; // Basic Information Realization Event
Public Event ErrorInfoHandler ErrorInfoout; // Error Information Realization Event
Public void exec () ////
{
Try
{
Int results = 0;
For (int i = 1; i <1000; i )
{
BaseInfoout (RETULT);
}
}
Catch (System.exception E)
{
ErrorInfoout (E.MESSAGE);
}
}
}
2) Implement information output in the form.
Private void Runbtn_Click (Object Sender, System.Eventargs E)
{
Test T = New Test ();
T.BaseInfoout = new baseinfohandler (baseinfoout);
T.ERRORINFOOUT = New ErrorInfoHandler (ErrorInfoout);
t.exec ();
}
Private Void Baseinfoout (String Str)
{
This.BaseInfolistBox.Items.Add (STR);
}
Private void ErrorInfoout (String Str)
{
THIS.ERRORINFOLISTBOX.ITEMS.ADD (STR);
}