-4

I am trying to return the Max value from asking a user to input a first and second value. I am also trying to utilize the Math.Max function.

class Return_Values
{
    public void RunExercise()
    {
        Console.WriteLine("First value?");
        int firstNumber = Int32.Parse(Console.ReadLine());

        Console.WriteLine("Second value?");
        int secondNumber = Int32.Parse(Console.ReadLine());

        int theMax;

        Max m = new Max();
        m.returnMax(firstNumber, secondNumber);                                       
        Console.WriteLine("The max of {0} and {1} is {2}", firstNumber, secondNumber, theMax);

    }
}

class Max
{
    public int returnMax(int firstNumber, int secondNumber)
    {
        int theMax = Math.Max(firstNumber, secondNumber);
        return theMax;
    }
}

I keep getting the error, Use of unassigned local variable 'theMax'.

pijoborde
  • 29
  • 7
  • The error is pretty self-explanatory. theMax is unassigned. – the.Doc Feb 03 '19 at 19:46
  • `m.returnMax(firstNumber, secondNumber);` returns an `int` but you are not capturing it! Try `int theMax = m.returnMax(firstNumber, secondNumber);` – JohnG Feb 03 '19 at 19:49
  • https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/cs0165 – devNull Feb 03 '19 at 19:54

1 Answers1

1

You forgot to actually assign theMax to void return variable

int theMax;

Max m = new Max();
theMax = m.returnMax(firstNumber, secondNumber); 
MaKiPL
  • 1,200
  • 8
  • 15