Click here to Skip to main content
15,890,185 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I have two methods with return type one being instance and the other one is static method. I am trying to access those methods with delegates but i am unable to access it i get a error " Has wrong return type

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Delgates_Ex1
{

    public delegate void ExampleDelgate (int x, int y);


    class Democlass
    {
        public int Add(int x, int y)

        {

            Console.WriteLine("Addition Method");
            return (x + y);
              
        }


        public static int mul(int x, int y)
        {
            Console.WriteLine("Multiplication Method");

            return (x * y);
        }

    }


    class Program
    {

        static void Main(string[] args)
        {
            Democlass d = new Democlass();

            ExampleDelgate ed = new ExampleDelgate(d.Add);

            ed(30, 10);

            Console.WriteLine("\n");

            ed = new ExampleDelgate(Democlass.mul);
            ed(30 * 10);

        }
    }
}
Posted

In addition to the correct Solution 1:

You don't need a separate delegate type declaration for such a simple signature; it is already done for you and is System.Func<int, int, int>. You don't need a separate type declaration at all:

C#
System.Func<int, int, int> ed = new System.Func<int, int, int>(Demo.Mul);


Moreover, you don't even need a separate (named) method implementation:
C#
System.Func<int, int, int> ed = new System.Func<int, int, int>( (x, y) => {
    return x * y;
});


The special feature of using lambda: you can make the like shown above local in some method, not exposed anywhere else. Why would you need such a thing? For just one thing, please see my short article: Hide Ad-hoc Methods Inside the Calling Method’s Body[^].

—SA
 
Share this answer
 
Comments
Thomas Daniels 30-May-14 11:38am    
Good addition, +5!
ProgramFOX has solved your problem, however you may also find this CodeProject article useful - C# - Delegates 101 - A Practical Example[^]
 
Share this answer
 
Comments
Thomas Daniels 30-May-14 10:11am    
That article will indeed be useful for the OP, +5!
CHill60 30-May-14 11:45am    
Thank you!
Change the return type of your delegate from void to int:
C#
public delegate int ExampleDelgate (int x, int y);

Also, when you try to access the mul method with a delegate, you should pass 30 and 10 as parameters and not as a multiplication:
C#
ed = new ExampleDelgate(Democlass.mul);
//ed(30 * 10); // remove this line
ed(30, 10); // add this
 
Share this answer
 
Comments
ShaHam11 30-May-14 10:07am    
thank you for the solution
Thomas Daniels 30-May-14 10:07am    
You're welcome!
Sergey Alexandrovich Kryukov 30-May-14 11:35am    
5ed. Please see some extra ideas in Solution 3.
—SA
Thomas Daniels 30-May-14 11:38am    
Thank you!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900