It tells me that it can't convert int to bool. Tried TryParse but for some reason the argument list is invalid.
Code:
private void SetNumber(string n)
{
// if user input is a number then
if (int.Parse(n))
{
// if user input is negative
if (h < 0)
{
// assign absolute version of user input
number = Math.Abs(n);
}
else
{
// else assign user input
number = n;
}
}
else
{
number = 0; // if user input is not an int then set number to 0
}
}
You could try something like below using
int.TryParse
..The reason there are complaints that it cannot convert an
int
to abool
is because the return type ofint.Parse()
is anint
and not abool
and in c# conditionals need to evaluatebool
values.Just use this:
if the parse was successful,
success
istrue
.If that case
i
contain the number.You probably got the
out
argument modifier wrong before. It has theout
modifier to indicate that it is a value that gets initialized within the method called.int.Parse will give you back an integer rather than a boolean.
You could use int.TryParse as you suggested.
int.Parse will convert a string to an integer. Current you have it within an if statement, so its treating the returned value of int.Parse as a bool, which its not.
Something like this will work: