0

I'm trying to convert a text input into an int that I can multiply and display the product, but whenever I try to parse the textbox contents as int, I get the "cannot convert bool to int" error message. Code is as follows:

private void button2_Click_1(object sender, EventArgs e)
{
    int Int1;
    int Int2;
    int Product;
    string Text1;
    string Text2;
    Text2 = textBox2.Text;
    Text1 = textBox1.Text;
    Int1 = int.TryParse(Text1, out Int1);
    Int2 = int.TryParse(Text2, out Int2);
    Product = Int1 * Int2;
    listBox1.Items.Add(textBox1.Text);
    listBox1.Items.Add(textBox2.Text);
    listBox1.Items.Add(Product);
}

Don't see where I'm going wrong.

Technovation
  • 397
  • 2
  • 12
A B
  • 37
  • 1
  • 1
  • 10

8 Answers8

3

This is happening because TryParse returns a bool the result is in the parameter with out, to solve you can remove the attribution.

int.TryParse(Text1, out Int1);
int.TryParse(Text2, out Int2);
Product = Int1 * Int2;

If you using TryParse you should use in a if to check if everything goes with success.

if(int.TryParse(Text1, out Int1) &&  int.TryParse(Text2, out Int2))
{        
    Product = Int1 * Int2;
    listBox1.Items.Add(textBox1.Text);
    listBox1.Items.Add(textBox2.Text);
    listBox1.Items.Add(Product);
}

If you don't need this behavior, you should consider using Parse, because it returns the parsed value.

Int1 = int.Parse(Text1);
Int2 = int.Parse(Text2);
Product = Int1 * Int2;
adricadar
  • 9,971
  • 5
  • 33
  • 46
  • It says I can accept in 7 minutes - does this restriction ever go away? – A B May 02 '15 at 10:14
  • 2
    No, will the restriction as a whole disappear after a set amount of reputation? (will I be able to accept answers without a time limit at some point later in time) – A B May 02 '15 at 10:18
  • 1
    @AB will never go away [see this answer](http://meta.stackoverflow.com/questions/250132/why-cant-i-accept-an-answer-in-the-first-15-minutes-after-asking-the-question) – adricadar May 02 '15 at 10:22
2

TryParse returns boolean, which tells you if the parse has succeeded or not. Try

int.TryParse(Text1, out Int1);
int.TryParse(Text2, out Int2);
TigOldBitties
  • 1,331
  • 9
  • 15
1

It is natural. TryParse returns boolean. out parameter is the result of TryParse. Check this post: How the int.TryParse actually works

Community
  • 1
  • 1
Ozan Deniz
  • 1,087
  • 7
  • 22
1

TryParse returns true or false based on its successful or unsuccessful attempt. if you are sure user enters number only, you can use

Int1=int.Parse(textBox1.Text)
Int2=int.Parse( textBox2.Text)
Technovation
  • 397
  • 2
  • 12
1

TryParse() returns bool, and out parameter will already have parsed value Int1 and Int2 if parsing was successful you have to do like this:

if(!int.TryParse(Text1, out Int1))
{
// show validation message
return;
}
if(!int.TryParse(Text2, out Int2))
{
// value not valid int
return;
}

TryParse() is used when we want to control program flow, you have to handle it using if block other wise on invalid input your program will throw exception

Input string was not in a correct format

Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
1

Your code is assigning the result of int.TryParse to your int. It cant because the return value is a bool indicating if the parsing worked.

Int1 = int.TryParse(Text1, out Int1);

Have a look at the documentation for details.

Pete Stensønes
  • 5,595
  • 2
  • 39
  • 62
1

Try this..

Int1 = int.Parse(textBox1.Text);
Rishad Appat
  • 1,786
  • 1
  • 15
  • 30
1

You should use the return value of TryParse to decide whether the parsing went successfully or not. It returns bool to indicate that the text has been converted to int, so you should change your code as follows:

private void button2_Click_1(object sender, EventArgs e) {
    int Int1;
    int Int2;
    int Product;
    string Text1;
    string Text2;
    Text2 = textBox2.Text;
    Text1 = textBox1.Text;
    listBox1.Items.Add(textBox1.Text);
    listBox1.Items.Add(textBox2.Text);
    if( int.TryParse(Text1, out Int1) && int.TryParse(Text2, out Int2)) {
        Product = Int1 * Int2;
        listBox1.Items.Add(Product);
    } else {
        listBox1.Items.Add("<incorrect input>");
    }
}

When the return value of one of TryParse calls is false, the product is not computed, and an error message "" is added to listBox1 instead of a product.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523