I want my small math program to look really sleek, and by this I mean under the Main
method I have the following methods:
Greet()
UserInput1()
UserInput2()
Result()
In Greet()
I just say "HI", in UserInput1()
I want to collect the first number, in UserInput2()
I want to collect the second number, and in Result()
I want to print the result of UserInput1 + UserInput2
. I can collect the number in UserInput 1 and 2
but I can’t seem to send them to Result()
without assigning values to them under the Main()
function.
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
Greet();
firstNumber();
secondNumber();
result(firstNumber, secondNumber);
Console.ReadKey();
}
public static void Greet()
{
Console.WriteLine("Hello, pls insert two numbers");
}
public static int firstNumber()
{
int num01 = Convert.ToInt32(Console.ReadLine());
return num01;
}
public static int secondNumber()
{
int num02 = Convert.ToInt32(Console.ReadLine());
return num02;
}
public static void result( int num01, int num02)
{
Console.WriteLine(num01 + num02);
}
}
}
change this:
result(firstNumber, secondNumber);
to this:
result(firstNumber(), secondNumber());
and remove the calls to the 2 methods in the two lines above.
To call a method without parameters, you need the parentheses without content.
Cannot convert from method group to int
This error message occurs when you attempt to take a method (without invocation) and pass it as a type. The result
method is expecting two parameters of type int
, but you're attempting to pass it the method, rather than the result of the method invocation.
You need to store the results in a variable, or invoke the methods with the ()
:
Like this:
static void Main(string[] args)
{
Greet();
var first = firstNumber();
var second = secondNumber();
result(first , second );
Console.ReadKey();
}
or this:
static void Main(string[] args)
{
Greet();
result(firstNumber(), secondNumber());
Console.ReadKey();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With