user3134679
user3134679

Reputation: 53

Is there something wrong with my If Statements?

This function checks if the values in the text boxes are parsable or not. This method is called upon in the method below this.

    private bool CheckForInvalidEntries()
    {
        bool ParseIsSuccessfull; int result; //These 2 variables are for trying to parse the entries in the Stat text boxes
        bool ContainsInvalidEntry = false;

        if ((ParseIsSuccessfull = int.TryParse(P1STRtextbox.Text, out result)) == false)
        {
            ContainsInvalidEntry = true;
        }
        else if ((ParseIsSuccessfull = int.TryParse(P1DEXtextbox.Text, out result)) == false)
        {
            ContainsInvalidEntry = true;
        }
        else if ((ParseIsSuccessfull = int.TryParse(P1VIGtextbox.Text, out result)) == false)
        {
            ContainsInvalidEntry = true;
        }
        else if ((ParseIsSuccessfull = int.TryParse(P1RMtextbox.Text, out result)) == false)
        {
            ContainsInvalidEntry = true;
        }
        else if ((ParseIsSuccessfull = int.TryParse(P1BMtextbox.Text, out result)) == false)
        {
            ContainsInvalidEntry = true;
        }
        else ContainsInvalidEntry = false;


        return ContainsInvalidEntry;
    }

This function is the event where if the process stat points button is clicked

    private void p1ProcessPointsBtn_Click(object sender, EventArgs e)
    {
        bool EntriesAreInvalid = new bool();

        EntriesAreInvalid = CheckForInvalidEntries();

        if (EntriesAreInvalid == true)
        {
            P1STRtextbox_TextChanged(sender, e);
            P1DEXtextbox_TextChanged(sender, e);
            P1VIGtextbox_TextChanged(sender, e);
            P1RMtextbox_TextChanged(sender, e);
            P1BMtextbox_TextChanged(sender, e);

        }
        else
        {
            MessageBox.Show("Success");
        }

The frame Im having problems with

FUNCTIONALITY: When the user presses the "Process Stat Points" button, the program checks whether the entries in the 5 text boxes are able to be parsed(in the CheckForInvalidEntries method). It then returns a bool value to the EntriesAreInvalid variable(in the p1ProcessPointsBtn_Click method). If the entries are not parsable, do action A, if the entries are parsable, do action B.

PROBLEM: If the numbers are parsable in all the text boxes, I don't get a result. Im only getting results if the text boxes are not parsable. I think it has something to do with the if statements within the "CheckForInvalidEntries" method. What can I do to fix my problem. Your time and effort is greatly appreciated!

Upvotes: 1

Views: 216

Answers (3)

Selman Genç
Selman Genç

Reputation: 101701

First of all you don't need to write your if statements like this, it's too messy and unnecessary. Complex code always prone to error,

Instead of this:

 if ((ParseIsSuccessfull = int.TryParse(P1STRtextbox.Text, out result)) == false)

You could write:

if(!int.TryParse(P1STRtextbox.Text, out result))

Because TryParse already returning a bool result.If you want check whether it's false just put negation operator (!) beginning of your statement. Also you can write a simple method to check whether your texts are parsable or not:

static bool CheckForParse(params string[] values) 
{
   int x;
   if(values.Lenght > 0)
   {
       for(int i=0; i<values.Lenght;i++)
       {
          if(!int.TryParse(values[i], x)) return false;
       }
       return true;

    } else { return false }

    return false;
}

And you can call it like this:

bool result = CheckForParse(P1STRtextbox.Text,
                            P1DEXtextbox.Text,
                            P1VIGtextbox.Text,
                            P1RMtextbox.Text,
                            P1BMtextbox.Text);
if(result)
{
   P1STRtextbox_TextChanged(sender, e);
   P1DEXtextbox_TextChanged(sender, e);
   ...
}

Upvotes: 0

Florian Kopremesis
Florian Kopremesis

Reputation: 120

i was too confused by your code. But i think this will help you. If i got you right you wanted to check if all the text in the textboxes is parsable to an int. And if it is so you wanted to print out "succes";

private bool isParsable(TextBox t) //takes a TextBox as paramater and returns true if
    {                              // its parsable
        int i = 4;
        if (int.TryParse(t.Text, out i) == true)
            return true;
        else
            return false;            
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        if(isParsable(tbox1) == true && isParsable(tbox2) == true) //if every textbox
        {                                                          //is parsable print
            tblock1.Text = "succes";
        }
        else
        {
            tblock1.Text = "error";
        }
    }

Upvotes: 1

HABO
HABO

Reputation: 15816

TryParse sets Result to zero if the conversion fails. Since you keep calling TryParse you keep resetting Result.

If you only want to check for parsing errors, this ought to work:

ContainsInvalidEntry = false;
ContainsInvalidEntry |= !int.TryParse(P1STRtextbox.Text, out result));
ContainsInvalidEntry |= !int.TryParse(P1DEXtextbox.Text, out result));
ContainsInvalidEntry |= !int.TryParse(P1VIGtextbox.Text, out result));
ContainsInvalidEntry |= !int.TryParse(P1RMtextbox.Text, out result));
ContainsInvalidEntry |= !int.TryParse(P1BMtextbox.Text, out result));
return ContainsInvalidEntry;

Aside: Comparing boolean values to true and false is a bit (Pardon the pun.) strange. if ( ( ParsedOkay == ( false ) ) ) may be valid, but if ( !ParsedOkay ) is more common.

Upvotes: 1

Related Questions