A B
A B

Reputation: 37

String is considered 'bool' after TryParse to int

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.

Upvotes: 0

Views: 1746

Answers (8)

adricadar
adricadar

Reputation: 10209

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;

Upvotes: 4

Technovation
Technovation

Reputation: 397

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)

Upvotes: 1

Ehsan Sajjad
Ehsan Sajjad

Reputation: 62488

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

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726579

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.

Upvotes: 1

Rishad Appat
Rishad Appat

Reputation: 1806

Try this..

Int1 = int.Parse(textBox1.Text);

Upvotes: 1

Pete Stens&#248;nes
Pete Stens&#248;nes

Reputation: 5665

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.

Upvotes: 1

TigOldBitties
TigOldBitties

Reputation: 1337

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

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

Upvotes: 2

Ozan Deniz
Ozan Deniz

Reputation: 1087

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

Upvotes: 1

Related Questions