-1

Hmmmm, I wonder why isn't this working...It gives an error stating "Use of unassigned local variable max". So, what is wrong with this code? I cant figure it out.

namespace ConsoleApplication1
{
    class Program
    {
        public int CalculateHighestNum(int value1, int value2, int value3) 
        {
            int max;

            if (value1 > (value2 & value3))
            {
                max = value1;
            }
            else if(value2 > (value1 & value3))
            {
                max = value2;
            }
            else if(value3 > (value1 & value2)) 
            {
                max = value3;
            }

            return max;
        }

        static void Main(string[] args) 
        {
            Console.Write("Enter first NUM : ");
            int a = Convert.ToInt32(Console.ReadLine());
            Console.Write("Enter second NUM : ");
            int b = Convert.ToInt32(Console.ReadLine());
            Console.Write("Enter third NUM : ");
            int c = Convert.ToInt32(Console.ReadLine());


            Program p = new Program();
            int highestnum = p.CalculateHighestNum(a, b, c);

            Console.WriteLine(highestnum + " = Highest Number");
        }
    }
aliadiere
  • 65
  • 1
  • 1
  • 10
  • 3
    Are you sure this does what you think it does? `if (value1 > (value2 & value3))` - might make sense in English... – John3136 Aug 08 '14 at 01:27
  • Try this for your requirement: int max = Math.Max(a, Math.Max(b, c)); Try to explore simpler ways. – SomeUser Aug 08 '14 at 01:31
  • John3136, I think that checks whether value1 is greater than value 2 and value3 – aliadiere Aug 08 '14 at 01:32
  • 3
    @user3497152 no it doesn't. it performs _bitwise and_ operation between value2 and value3 then it checks if value1 is higher than the result of the AND operation. – Selman Genç Aug 08 '14 at 01:34
  • If you want to persist with this logic, try if((value1 > value2) && (value1 > value3)). – SomeUser Aug 08 '14 at 01:45

1 Answers1

4

You need to set an initial value to max, or any other local variable, before you can use it.

int max = 0;

The reason for this is to reduce the chance of using a variable without assigning it a sensible default, and because the variable needs to be assigned to something before being returned. (In this case, if your if statements are all false)

The compiler will not show this error if you assign a value to the variable in all cases, such as in an else statement.

Also, as @Partha explained in the comments, you could simplify your logic to just:

return Math.Max(value1, Math.Max(value2, value3));
Cyral
  • 13,999
  • 6
  • 50
  • 90
  • To clarify a bit, theoretically, your `if/else` statements might not hit, so then you would `return max` where `max` is not initialized – Prescott Aug 08 '14 at 01:27
  • It will compile, but return wrong max value. Actually it might be good to keep `max` initially uninitialized in such cases. – AlexD Aug 08 '14 at 01:29
  • Added your advice @GrantWinney – Cyral Aug 08 '14 at 01:30
  • 2
    for variety, here is another alternative `return new[] { value1, value2, value3 }.Max();` – Selman Genç Aug 08 '14 at 01:36
  • @GrantWinney Absolutely. I meant that if we know that one of `if - else` branches must initialize `max`, then we may intentionally skip `int max = 0`. So if by mistake we do not cover all cases, we get the compilation error. And compilation error is much better than a function which compiles, but misbehaves by returning `0` in some cases. – AlexD Aug 08 '14 at 02:01