user14036292
user14036292

Reputation:

Method name expected Delegate

I am trying to make a calculator with delegates and classes, but I am facing this problem when I try to refer my class to delegate it gives me an error called "Method name expected"

This is my delegate public delegate int Conculate();

and this is where I want to execute it:

Conculate conculate = new Conculate(action.Addition(a, b)); but I am getting an error here action.Addition(a, b) showing me "Method name expected " here's my fool program:

class Program
    {

        static void Main(string[] args)
        {
            int a = 0, b = 0;
            Actions action = new Actions();
            Console.Title = "Calculator";
            bool isCountable = true;
           
            
            Dictionary<string, string> actionDictionary = new Dictionary<string, string>()
            {
                {   "+", "addition" },
                {"-", "substraction" },
                {"*","multiplication" },
                { "/", "division"}
            };

            while (isCountable)
            {

               
                Console.WriteLine("Enter your numbers");
                try
                {
                    a = int.Parse(Console.ReadLine());
                    b = int.Parse(Console.ReadLine());
                }
                catch (Exception)
                {
                    Console.WriteLine("The string hasn't been convented to int!!");
                    continue;
                }
                Console.Clear();
                Console.WriteLine("You can only use +, -, /, *");
                Console.WriteLine("So choose a fucnion above");

                var conc = new Conculate(action);

                string execution = Convert.ToString(Console.ReadLine());
               
                    switch (execution)
                    {
                        case "+":
                            Console.WriteLine(action.Message(actionDictionary[execution]));
                            Console.WriteLine(action.Addition(a, b));
                            break;
                        case "-":
                            Console.WriteLine(action.Message(actionDictionary[execution]));
                            Console.WriteLine(action.Sub(a, b));
                            break;
                        case "*":
                            Console.WriteLine(action.Message(actionDictionary[execution]));
                            Console.WriteLine(action.Mult(a, b));
                            break;
                        case "/":
                            Console.WriteLine(action.Message(actionDictionary[execution]));
                            action.Division(a, b);
                            break;
                        default:
                            Console.WriteLine("There's no such an option");
                            break;
                    }

                
               
                isCountable = action.Answer();
            }
        }
    }

and this is where I try to call a delegate from another class to this class

public delegate int Conculate(int a, int b); 
  public class Actions
    {

        public bool Answer()
        {
            string message = "Do you want to continue?";
            Console.WriteLine(message);
            string answer = Convert.ToString(Console.ReadLine());
            if (answer != "y")
            {
                Console.WriteLine("Bye!");
                return false;
            }
            return true;

        }
        public int Addition(int a, int b)
        {
            return a + b;
        }
        public int Sub(int a, int b)
        {
            return a - b;
        }
        public int Mult(int a, int b)
        {
            return a * b;
        }
        public void Division(float a, float b)
        {
            float d;
            if (b == 0)
                Console.WriteLine("You cannot devide this number by 0");
            else
            {
                d = a / b;
                Console.WriteLine(d);
            }
        }
        public string Message(string execution, string message = "Your answer is...")
        {
            Console.WriteLine($"You've chosen the {execution} option");
            return message;
        }
    }

Upvotes: 0

Views: 531

Answers (2)

Mosia Thabo
Mosia Thabo

Reputation: 4267

The constructor of the delegate is expecting a method name. In other works, you aren't supposed to pass a call of the referenced method to delegate contructor.

I haven't actually performed delegates and used a method that exist outside of the class where that delegate is defined. But I had just done a .NetFiddle so that I could troubleshoot your issue. So let's see how we can sort it out.

Please try do the following:

Conculate conculate = new Conculate(action.Addition);

You can even shortten this by assigning just the parameter itself:

Conculate conculate = action.Addition;

And when you call your delegate, you will then pass the params conculate(a, b).

Here's a working .NetFiddle


Just make sure that action is an instance of a public Actions class.

Upvotes: 0

AnGG
AnGG

Reputation: 831

This line

action.Addition(a, b)

returns int not a function, you should create a delegate from it like this :

Conculate conculate = () => action.Addition(num1, num2);

This is working, you need to call the with parameters

public class ConsoleApp2
{
    public delegate int Conculate();

    public static void Main(string[] args)
    {
        Class2 action = new Class2();
        int num1 = 10, num2 = 20;

        Conculate conculate = () => action.Addition(num1, num2);
    }
}

public class Class2
{
    public int Addition(int num1, int num2)
    {
        return num1 + num2;
    }
}

Upvotes: 1

Related Questions