delegates – settings function on the fly

Delegates allow a virtual method/function (static method) to be called via a link (which is the delegated variable). I have done a post before about delegates but kinder think and also have been asked how do you link one to a class and also change the delegated function due to a user input, so here goes.

I have started with setting up what type of method that I want to be associated with the delegated variable, this will be a mathematical function of either addition or subtraction with using integer values, so we need to return a integer value and also pass in two integer parameters, so a new delegated virtual method would be

public delegate int mathsOp(int value1, int value2);

and here would be a example of a method that the delegate is able to link to, because it takes 2 integer parameters and returns a integer value

public int add(int value1, int value2)
{
	return value1 + value2;
}

so we now have the delegate declaration and also a method to be able to point to, so we now need to setup the variables, one for the actual functions (MathClass that is holding the subtraction and addition methods) and also the delegated variable theMathOp that is a delegated type.

MathClass mathFunc = new MathClass();
 
mathsOp theMathOp;

to actually set the method up on the fly, you just need to tell it where you want the delegated type to point to

theMathOp = new mathsOp(mathFunc.add);

and all is needed to call the delegated type variable, well you just call it like any other method

theMathOp(inputValue1, inputValue2);

that is about it, here is the code in full that will take in some values from the user and also the user is able to choose between addition and subtraction methods

using System;
 
namespace newDelegates
{
	// a maths holding delegated function
	public delegate int mathsOp(int value1, int value2);
 
	class MathClass
	{
		// the functions to call within the class
		public int add(int value1, int value2)
		{
			return value1 + value2;
		}
 
		public int sub(int value1, int value2)
		{
			return value1 - value2;
		}
 
	}
 
	class MainClass
	{
		public static void Main (string[] args)
		{
 
			// setup the maths class which has the functions inside to call
			MathClass mathFunc = new MathClass();
 
			mathsOp theMathOp;
 
			// there is no error checking in the inputs!!
 
			Console.Write("Please enter value 1 : ");
			int inputValue1 = Convert.ToInt16(Console.ReadLine());
			Console.Write("Please enter value 2 : ");
			int inputValue2 = Convert.ToInt16(Console.ReadLine());
 
			Console.WriteLine("Please enter maths function :");
			Console.WriteLine("1 : add");
			Console.WriteLine("2 : sub");
			int mathsInputFun = Convert.ToInt16(Console.ReadLine());
			// setup the virtual function to which ever the user wants
			switch (mathsInputFun)
			{
				case 1 : 
					theMathOp = new mathsOp(mathFunc.add); 
					break;
				case 2 : 
					theMathOp = new mathsOp(mathFunc.sub); 
					break;
				default :
					Console.WriteLine("Settings to add");
					theMathOp = new mathsOp(mathFunc.add); 
					break;
			}
 
			Console.WriteLine("Value 1= " + inputValue1 + (mathsInputFun == 1 ? " + " : " - ") 
			                  + " value 2 = " + inputValue2 + " = ");
 
			// here we call the virtual function that was setup
			Console.WriteLine(theMathOp(inputValue1, inputValue2));
		}
	}
}

and below is the output of two runs of the program, one using addition and the other using subtraction

Please enter value 1 : 3
Please enter value 2 : 2
Please enter maths function :
1 : add
2 : sub
1
Value 1= 3 +  value 2 = 2 = 5
 
second run through
 
Please enter value 1 : 5
Please enter value 2 : 2
Please enter maths function :
1 : add
2 : sub
2
Value 1= 5 -  value 2 = 2 = 3

Leave a Reply

Your email address will not be published. Required fields are marked *