9

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  
    }
}
2
  • Is number a bool or something? Where are you getting an error about not being able to convert int to bool? Commented Mar 22, 2011 at 17:53
  • Why the vb-style comments in C# code? Are you a VB coder coming into C#? Or a newbie putting together samples from different languages? Commented Mar 22, 2011 at 17:57

10 Answers 10

18

You were probably very close using TryParse, but I'm guessing you forgot the out keyword on the parameter:

int value;
if (int.TryParse(n, out value))
{

}
Sign up to request clarification or add additional context in comments.

1 Comment

Indeed; int.Parse() returns an int, which is why you can't check it for true or false without evaluating the returned value in some way.
8

Just use this:

int i;
bool success = int.TryParse(n, out i);

if the parse was successful, success is true.

If that case i contain the number.

You probably got the out argument modifier wrong before. It has the out modifier to indicate that it is a value that gets initialized within the method called.

Comments

4

You can try with some simple regular expression :

  bool IsNumber(string text)
    {
      Regex regex = new Regex(@"^[-+]?[0-9]*\.?[0-9]+$");
      return regex.IsMatch(text);
    }

Comments

3
    private void SetNumber(string n)
    {
        int nVal = 0;

        if (int.TryParse(n, out nVal))
        {
            if (nVal < 0)
                number = Math.Abs(nVal);
            else
                number = nVal;
        }
        else
            number = 0;
    }

Comments

2

There are a lot of problems with this code:

  • Using VB-style line comments (') instead of C# slashes
  • Parse for integer returns an int and not a bool
  • You should use TryParse with an out value
  • h does not seem to be valid at all. Is it a type for n?
  • There are variables that do not seem to be defined in function scope (number) are they defined at class scope?

But try this:

private void SetNumber(string n)
{
    int myInt;
    if (int.TryParse(n, out myInt)) //if user input is a number then
    {
        if (myInt < 0) //if user input is negative
            number = Math.Abs(n); //assign absolute version of user input
        else //else assign user input
            number = n;
    }
    else number = 0; //if user input is not an int then set number to 0
}

Comments

1

You could try something like below using int.TryParse..

        private void SetNumber(string n)
        {
            int parsed = -1;
            if (int.TryParse(n, out parsed)) //if user input is a number then
            ...

The reason there are complaints that it cannot convert an int to a bool is because the return type of int.Parse() is an int and not a bool and in c# conditionals need to evaluate bool values.

Comments

1

int.Parse will give you back an integer rather than a boolean.

You could use int.TryParse as you suggested.

int parsedValue;
if(int.TryParse(n, out parsedValue))
{
}

Comments

1

Well for one thing the inner if statement has an 'h' instead of an 'n' if(h < 0). But TryParse should work there assuming that 'number' is a class variable.

 private void SetNumber(string n)
    {
        int temp;
        bool success = Int32.TryParse(n, out temp);

        // If conversion successful
        if (success)
        {
            // If user input is negative
            if (temp < 0)
                number = Math.Abs(temp); // Assign absolute version of user input
            else // Assign user input
                number = temp;

        }
        else
        {
            number = 0;
        }

    }

Comments

0

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:

private void SetNumber(string n)
{
    int num = 0;
    try{
        num = int.Parse(n);
        number = Math.Abs(num);
    }catch(Exception e){
        number = 0;
    }   
}

1 Comment

forgot about TryParse, haven't used C# recently see @Paul Sasik for the proper example.
0

I did this in the simplest way I knew how.

 static void Main(string[] args)
    {
        string a, b;
        int f1, f2, x, y;
        Console.WriteLine("Enter two inputs");
        a = Convert.ToString(Console.ReadLine());
        b = Console.ReadLine();
        f1 = find(a);   
        f2 = find(b);   

        if (f1 == 0 && f2 == 0)
        {
            x = Convert.ToInt32(a);
            y = Convert.ToInt32(b);
            Console.WriteLine("Two inputs r number \n so tha additon of these text box is= " + (x + y).ToString());
        }
        else
            Console.WriteLine("One or tho inputs r string \n so tha concadination of these text box is = " + (a + b));
        Console.ReadKey();
    
    }
        static int find(string s)
        {
        string s1 = "";
        int f;
         for (int i = 0; i < s.Length; i++)
            for (int j = 0; j <= 9; j++)
            {
                string c = j.ToString();
                if (c[0] == s[i])
                {
                    s1 += c[0];
                }
            }
        
        if (s==s1)
            f= 0;
        else
            f= 1;

        return f;
    }

2 Comments

please take a minute to explain the changes/improvements you have made, so the author or other people have a chance to learn from it. unrelated: welcome to stackoverflow :)
"Simple way" is more complex than pretty much every other answer here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.