Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to pass delegate as a parameter

How to pass delegate as a parameter

Scheduled Pinned Locked Moved C#
csharphelptutorial
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Chiman1
    wrote on last edited by
    #1

    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.

    A L 2 Replies Last reply
    0
    • C Chiman1

      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.

      A Offline
      A Offline
      Abhinav S
      wrote on last edited by
      #2

      One way to do this would be using the Action tag. Have a look here[^].

      Me, I'm dishonest. And a dishonest man you can always trust to be dishonest.
      Honestly. It's the honest ones you want to watch out for...

      1 Reply Last reply
      0
      • C Chiman1

        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.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Delegates are not equivalent when they just share the same signature, you could do this if you declare only one delegate type and use that in both places. Delegate declarations do not have to be inside a class.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups