Delegated use example

Source: Internet
Author: User

I. Example: the user inputs two operands and then selects an operation method (addition, subtraction, multiplication, division ),ProgramYou can calculate the result. The key of this example is that it uses the delegate to dynamically call different methods at runtime.

1. First define a private field curopt of the calculatedelegate type, which will reference the method for completing different computing tasks:

 
//Declare delegate type
Public Delegate DoubleCalculatedelegate (DoubleX,DoubleY );


//Current Operation Type
PrivateCalculatedelegate curopt;

2. Define a method to receive a delegate:

  //   computing   
void docalculate (calculatedelegate calmethod)
{< br> double X, Y;
try
{< br> X = convert. todouble (txtnumber1.text);
Y = convert. todouble (txtnumber2.text);
lblinfo. TEXT = string. format ( " result: {0} ", calmethod (x, y ));
}< br> catch (exception e)
{< br> lblinfo. TEXT = string. format ( " result: illegal data ");
}< BR >}

The preceding method receives a calculatedelegate parameter. With this parameter, the docalculate method can dynamically call the addition, subtraction, multiplication, and division of four methods defined in the sample program.

 # Region "Four methods to complete the operation"

Double Add ( Double X, Double Y)
{
Return X + Y;
}
Double Subtract ( Double X, Double Y)
{
Return X-y;
}
Double Multiply ( Double X, Double Y)
{
Return X * Y;
}
Double Divid ( Double X, Double Y)
{
Return X/y;
}

# Endregion

 

The following is a dynamic selection of operations to determine which method to call for calculation.

 Private   Void Txtnumber1_textchanged ( Object Sender, eventargs E)
{
Docalculate (curopt );
}

Private Void Txtnumber2_textchanged ( Object Sender, eventargs E)
{
Docalculate (curopt );
}

Private Void Rdoadd_checkedchanged ( Object Sender, eventargs E)
{
Curopt = This . Add;
Docalculate (curopt );
}

Private Void Rdosubtract_checkedchanged ( Object Sender, eventargs E)
{
Curopt =This . Subtract; // Select Function
Docalculate (curopt ); // Call a function
}

Private Void Rdomultiply_checkedchanged ( Object Sender, eventargs E)
{
Curopt = This . Multiply;
Docalculate (curopt );

}

Private Void Rdodivid_checkedchanged ( Object Sender, eventargs E)
{
Curopt = This . Divid;
Docalculate (curopt );
}

 

2. Use a delegate to implement multi-form communication.

Click the from form button. The main form records the number of clicks on the button.

The showinfodelegate delegation is used to transmit information between master and slave forms. The definitions are as follows:

//Declare a delegate type
Public Delegate VoidShowinfodelegate (StringInfo );

 

A public delegate type field is defined from the form, which references a method used to record information:

 
 PublicShowinfodelegate recorder;

 

Click the button in the form to accumulate the counter and notify the recorder

Private IntCounter =0;//Counter
Private VoidBtnclickme_click (ObjectSender, eventargs E)
{
Counter ++;
If(Recorder! =Null)
Recorder (counter. tostring ());
}

The record method in the main form is used to complete record operations:

//Display information with tags. Note that this method can be private
PrivateVoidRecord (string info)
{
Lblcount. Text = Info;
}

The following are the main form constructors:CodeThe above code is passed to the recorder field of the form to build a bridge for information exchange between master and slave form objects.

PublicFrmmain ()
{
Initializecomponent ();
//Create and display an object from the form
Frmother FRM =NewFrmother ();
FRM. Recorder =This. Record;//Assign a value to the delegate variable from the form
FRM. Show ();

}

 

For more information about delegated technology, see another article.

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.