using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Delegate_Example_1
{
public partial class Form1 : Form
{
/*
*What is a delegate?
*-------------------
*a delegate object is a reference to a method.
*(In c++, we use the term, function pointer)
*All delegate objects in .Net are multicastdelegates, that means a delegate object may reference multiple methods.
*A delegate object contains a list of references to methods.
*In general, a delegate is created to reference a single method.
*
* Step 1: Define a delegate type (This is the same way as defining a class type
* Step 2: Create a delegate object
* Step 3: Invoke the delegate object, or Use it as a parameter to another method
*
* Internally a delegate type is a class that inherits from the .Net MultiCastDelegates.
* However, a delegate type definition has different syntax than a class definition
* (internally, the compiler turns your delegate type into a class that inherits
* from the Multicast delegate
*
* Delegate type syntax definition
* *******************************
* public delegate return_type DelegateName(parameters list);
*
* In a delegate type definition, you define a method signature, which consists of return type and
* the list of parameters
* The signature must be similar to the method that this delegate will reference.
*
*/
//Example: Defining a delegate type for methods that return a double
//and take 2 parameters of type double (name of the parameters is not important)
public delegate double OperationHandler(double a, double b);
//OperationHandler is the delegate type. The compiler will turn it into
//a class that inherits from the .Net MultiCastDelegate class
//This delegate type is defined to handle only methods that have the
//same signature (return a double and take 2 params of type double)
//Declare a delegate object
OperationHandler ophandler;
OperationHandler ophandler2;
public Form1()
{
InitializeComponent();
//ophandler = new OperationHandler(Mul); //older syntax
//or the newer syntax is
ophandler = Mul;
ophandler2 = Add;
}
//here is a method that has the same signature as the OperationHandler delegate
private double Mul(double x, double y)
{
double result = x * y;
return result;
}
//another method that has samesignature as the OperationHandler delegate type
private double Add(double x, double y)
{
double result = x + y;
return result;
}
private void btnMul_Click(object sender, EventArgs e)
{
//generate two random values
double x = new Random().Next(100, 200);
double y = new Random().Next(100, 200);
//Invoke(call) the opHandler delegate
//Invoking a delegate object is the same thing as calling a method. (similar syntax)
//When you invoke a delegate, it will call all the methods it has references to.
double result = ophandler(x, y);
double addResult = ophandler2(x, y);
//the ophandler delegate object calls the method Mul and passes to it x and y
//the method Mul does the computation, returns the result to the delegate
//ophandler, and the ophandler delegate returns the results to us
richTextBox1.Text = String.Format("{0} * {1} = {2}", x, y, result);
richTextBox1.AppendText("\n\n");
richTextBox1.AppendText(String.Format("{0} + {1} = {2}", x, y, addResult));
}
//-------------------------------------------------------------------------------------
Random rand = new Random();
private void btnDivSub_Click(object sender, EventArgs e)
{
//generate two random values
double x = rand.Next(100, 200);
double y = rand.Next(100, 200);
//create a OpereationHandler delegate object
OperationHandler opHand = Divide;
//invoke the delegate object
double divResult = opHand(x, y);
//reassign opHand to referance Sub method
opHand = Sub;
double subResult = opHand(x, y);
richTextBox1.Text = String.Format("{0} / {1} = {2}", x, y, divResult);
richTextBox1.AppendText("\n\n");
richTextBox1.AppendText(String.Format("{0} - {1} = {2}", x, y, subResult));
}
private double Divide(double x, double y)
{
return x / y;
}
private double Sub(double x, double y)
{
return x - y;
}
}
}
//Lab Assignment: Due @Thursday Jan 7th, 15
//New Project
//Define a delegate type that takes an array of integers and returns a double
//Define a method ComputeAverage to compute and return the average value of an array of integers
//Define a method ComputeAvgOfOddNegatives to compute and return the average value of all the odd negative values in an array
//Add a button, within the button creates a delegate object of the first method invoke it and display its results
//Re-assignthe delegate object to the second method, Invoke it and display its results

No comments:
Post a Comment