Home > database >  How do I set a value in one method and then return that value to another method?
How do I set a value in one method and then return that value to another method?

Time:02-18

I need some help. If I understand this right, the value of "int F" gets sent to "FtoC" converted and then returned to the MenuSelect1 method. Now I want to return or save the value "int C" has after its been converted to the MenuSelect2 method? I have tried everything I can think of, but I just get errors. (I have reset the code to original state now where MenuSelect1 and 2 är void). Whats an easy way to solve this? Thank you.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


  public static class SecondClass {


  public static int FtoC(int fahrenheit)
  {
    int C = ((fahrenheit - 32) * 5) / 9;
    return C;
  }

  
  public static void MenuSelect1()
  {
    while (true)
    {
      int F = 0;
      Console.Write("\nType how many degrees Fahrenheit you want: ");

      try
      {
        F = Convert.ToInt32(Console.ReadLine());
      }
      
      catch (Exception e)
      {
        Console.ForegroundColor = ConsoleColor.DarkRed;
        Console.WriteLine("ERROR!");
        Console.ForegroundColor = ConsoleColor.White;
      }

      int C = SecondClass.FtoC(F);

      if (C < 80 || C > 87)
      {
        Console.WriteLine("Try a different temperature.");
      }
      else
      {
        Console.WriteLine(C   "°C is perfect. Start the sauna and enjoy!");
        break;
      }
    }
    Console.ReadKey();
  }

   
  public static void MenuSelect2()
  {
    Console.WriteLine("Starting Sauna. Wait for it to reach desired temperature...");
    Console.Write("Temperature: {0}");
    Console.ReadKey();
  }
    

}

CodePudding user response:

You could also save the value of C as a property / field in your class. Here is an example of saving it as a field called _celcius:

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


  public static class SecondClass {

  private static int _celcius = 0;  // <--- Your field

  public static int FtoC(int fahrenheit)
  {
    int C = ((fahrenheit - 32) * 5) / 9;
    return C;
  }

  
  public static void MenuSelect1()
  {
    while (true)
    {
      int F = 0;
      Console.Write("\nType how many degrees Fahrenheit you want: ");

      try
      {
        F = Convert.ToInt32(Console.ReadLine());
      }
      
      catch (Exception e)
      {
        Console.ForegroundColor = ConsoleColor.DarkRed;
        Console.WriteLine("ERROR!");
        Console.ForegroundColor = ConsoleColor.White;
      }

      _celcius = SecondClass.FtoC(F);   // <--- Assign field here

      if (_celcius  < 80 || _celcius  > 87)
      {
        Console.WriteLine("Try a different temperature.");
      }
      else
      {
        Console.WriteLine(_celcius   "°C is perfect. Start the sauna and enjoy!");
        break;
      }
    }
    Console.ReadKey();
  }

   
  public static void MenuSelect2()
  {
    Console.WriteLine("Starting Sauna. Wait for it to reach desired temperature...");
    Console.Write("Temperature: {0}", _celcius); // <--- Use your field here
    Console.ReadKey();
  }
    

}```

CodePudding user response:

You either call the MenuSelect2() directly from the MenuSelect1()

public static void MenuSelect1()
{
    while (true)
    {
      int F = 0;
      Console.Write("\nType how many degrees Fahrenheit you want: ");

      try
      {
        F = Convert.ToInt32(Console.ReadLine());
      }
      
      catch (Exception e)
      {
        Console.ForegroundColor = ConsoleColor.DarkRed;
        Console.WriteLine("ERROR!");
        Console.ForegroundColor = ConsoleColor.White;
      }

      int C = SecondClass.FtoC(F);

      if (C < 80 || C > 87)
      {
        Console.WriteLine("Try a different temperature.");
      }
      else
      {
        Console.WriteLine(C   "°C is perfect. Start the sauna and enjoy!");
        
        // here
        MenuSelect2(C);
        
        break;
      }
    }
    Console.ReadKey();

}

public static void MenuSelect2(int C)
{
    Console.WriteLine("Starting Sauna. Wait for it to reach desired temperature...");
    Console.Write($"Temperature: {C}");   // <- notice the $ for interpolated strings.
}

Or return the value from the MainSelect1() to the caller which calls the MainSelect2() method.

public static void Main()
{
    // get the temperature
    int C = MenuSelect1();

    // pass it to the other method.
    MenuSelect2(C);

    Console.ReadKey();
}


public static int MenuSelect1()  // <- change the signature (return int)
{
    while (true)
    {
      int F = 0;
      Console.Write("\nType how many degrees Fahrenheit you want: ");

      try
      {
        F = Convert.ToInt32(Console.ReadLine());
      }
      
      catch (Exception e)
      {
        Console.ForegroundColor = ConsoleColor.DarkRed;
        Console.WriteLine("ERROR!");
        Console.ForegroundColor = ConsoleColor.White;
      }

      int C = SecondClass.FtoC(F);

      if (C < 80 || C > 87)
      {
        Console.WriteLine("Try a different temperature.");
      }
      else
      {
        Console.WriteLine(C   "°C is perfect. Start the sauna and enjoy!");
        
        return C; // return the value to the caller.
      }
    }
}

public static void MenuSelect2(int C)
{
    Console.WriteLine("Starting Sauna. Wait for it to reach desired temperature...");
    Console.Write($"Temperature: {C}");   // <- notice the $ for interpolated strings.
}

I rather use the second suggestion, because this way the MenuSelect1() isn't tightcoupled to MenuSelect2() and can be reused for other purposes.

CodePudding user response:

The problem at this point is that the variable C is instantiated and stored inside the method MenuSelect1(), what you should do is create a class variable like int fahrenheitValue and then inside the menuSelect1 method use this.fahrenheitValue = C so the value is stored inside the class variable and then you can access it from everywhere

CodePudding user response:

All the variables that are defined in MenuSelect1() will NOT be visible as soon as the method ends.

You can define a static property in your "SecondClass"

private static int degreesCelsius;

Then you can set this property in

if (C < 80 || C > 87)
{
    Console.WriteLine("Try a different temperature.");
}
else
{
    Console.WriteLine(C   "°C is perfect. Start the sauna and enjoy!");
    degreesCelsius = C;
    break;
}

In your MenuSelect2() method you can use it.

Console.Write("Temperature: {0}", degreesCelsius);
  • Related