14 August 2012

Use Multiple Cast Delegates in C# .net

First create a class file for using Method in Delegate.Class name is  :Mathamatic.cs
Function Names are Add,Sub,Mul,Div.
Functions are given below:
public static string Add(int i, int j)
       {
          return "Sum of  I and J  is " + " " + ":" + (i + j);
       }
public static string Sub(int i, int j)
        {
            return "Sub of  I and J  is " + " " + ":" + (i - j);
        }
public static string Mul(int i, int j)
{
            return "Mul of  I and J  is " + " " + ":" + (i * j);
}
public static string Divi(int i, int j)
{
       return "Divide of  I and J  is " + " " + ":" + (i / j);
}



In our Form , drag and drop a button control in the Form.
First Declare a Delegate in the form :(like this)


public delegate string matha (int i,int j);


Now paste this code in the button Click Event.


 matha matadd = new matha(mathamatics.Add);
 MessageBox.Show(matadd(10, 5).ToString());
 matha matSub = new matha(mathamatics.Sub);
 MessageBox.Show(matSub(10, 5).ToString());
 matha matDivi = new matha(mathamatics.Divi);
 MessageBox.Show(matDivi(10, 5).ToString());
 matha matMul = new matha(mathamatics.Mul);
 MessageBox.Show(matMul(10, 5).ToString());


Happy Coding!

No comments: