How to pass delegate as a parameter
-
Hi, I am new to C# , Please suggest the approach. I have written below code . public class A { public B obj = new B(); public delegate int delegateCalculate(int i, int j); delegateCalculate dc ; public int Add(int Num1 , int Num2) { return (Num1 + Num2); } public int Sub(int Num1, int Num2) { return (Num1 - Num2); } public void Display() { dc = this.Sub; obj.Result(dc); //error } } public class B { public int Num1 = 10 ; public int Num2 = 20 ; public delegate int Calc (int i,int j); public int Result(Calc dc) { int ii = dc(Num1, Num2); return ii; } } The above code gives error for obj.Result(dc); call. Please suggest how to call Result() of class B in class A.
-
Hi, I am new to C# , Please suggest the approach. I have written below code . public class A { public B obj = new B(); public delegate int delegateCalculate(int i, int j); delegateCalculate dc ; public int Add(int Num1 , int Num2) { return (Num1 + Num2); } public int Sub(int Num1, int Num2) { return (Num1 - Num2); } public void Display() { dc = this.Sub; obj.Result(dc); //error } } public class B { public int Num1 = 10 ; public int Num2 = 20 ; public delegate int Calc (int i,int j); public int Result(Calc dc) { int ii = dc(Num1, Num2); return ii; } } The above code gives error for obj.Result(dc); call. Please suggest how to call Result() of class B in class A.
-
Hi, I am new to C# , Please suggest the approach. I have written below code . public class A { public B obj = new B(); public delegate int delegateCalculate(int i, int j); delegateCalculate dc ; public int Add(int Num1 , int Num2) { return (Num1 + Num2); } public int Sub(int Num1, int Num2) { return (Num1 - Num2); } public void Display() { dc = this.Sub; obj.Result(dc); //error } } public class B { public int Num1 = 10 ; public int Num2 = 20 ; public delegate int Calc (int i,int j); public int Result(Calc dc) { int ii = dc(Num1, Num2); return ii; } } The above code gives error for obj.Result(dc); call. Please suggest how to call Result() of class B in class A.