wygekas
wygekas

Reputation: 1

Calling delegate result in error "unasigned"

I want to write on screen result i get after completing actions at the bottom of the code. But i don't want to use Console.WriteLine() in "IF" function i want to call it as delegate carying value but it says unasigned. (site says mostly code but i dont have to say anything else so i just type something here :D )

namespace ConsoleApp5
{
    class program
    {
        delegate int first(int a, int b);  
        static void Main()
        {
            first beta;
            int result;
            Console.Write("insert number A: ");
            int.TryParse(Console.ReadLine(), out int a);
            if (a == 0)
            {
                Console.WriteLine("not a number");
                Environment.Exit(0);
            }

            Console.Write("insert number B: ");
            int b = int.Parse(Console.ReadLine());
            if (a == 0)
            {
               Console.WriteLine("not a number");
               Environment.Exit(0);
            }

            Console.WriteLine("plus(1) or minus (0)");  
            int c = int.Parse(Console.ReadLine());

            if (c == 1)
            {
                beta = plus;
                result = beta(a, b);  
                Console.WriteLine(result);
            }
            else if (c == 0)
            {
               beta = minus; 
               result = beta(a, b); 
               Console.WriteLine(result);
            }

            beta(); // PROBLEM HERE, I WANT TO WRITE ANSWER FROM THIS 
                    //instead of "Console.WriteLine(result);" inside function

       }

        private static int plus(int a, int b)
        {
            return a + b;
        }

        private static int minus(int a, int b)
        {
            return a - b;
        }
    }
}

Example what i mean i want to accomplish but a bit in different way.

using System;
namespace consoleApplication4
{
  class Program{

Delegate void Message();

     static void Main (string[] args)
{

Message mes;

if (DateTime.Now.Hour < 12)
{
mes=GoodMorning;
}

else{
mes=GoodEvening;
}

mes();  //this does what i want here but in code above something is missing
Console.ReadKey();
}


private static void GoodMorning(){
console.WriteLine("Good Morning");
}
Private static void GoodEvening(){
Console.WriteLine("Good Evening");
}}}

Upvotes: 0

Views: 57

Answers (1)

Alexei Levenkov
Alexei Levenkov

Reputation: 100545

You already have correct code to call your delegate in both branches of if. So to get code almost working those two lines calling beta need to be moved out of both branches:

    if (c == 1)
    {
       beta = plus;
    }
    else if (c == 0)
    {
       beta = minus; 
    }
    result = beta(a, b); 
    Console.WriteLine(result);

Now the other problem is still there - your if checks have 3 outcomes (0, 1, other) but beta is only assigned in two of cases. So we need to add that "other" case too with some desired output (or simply failure), switch statement expresses it better than chained ifs:

    switch (c)
    {
      case 1: beta = plus; break;
      case 0: beta = plus; break;
      default: beta = (a,b)=>""; break;
      // alternatively to fail: default: return 0;
    }
    result = beta(a, b); 
    Console.WriteLine(result);

Upvotes: 1

Related Questions