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();
}
}
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();
}
}```