If the state logic doesn't work

I have added one label on the form that is not visible to the user. Next, add text containing these labels.

Here is my logic, but it fails. I wanted so badly, if the label contains "No match" or "Timeout" should not continue.

If((!label.Text.Contain("No match")) || label.Text.Contain("Time out"))
{
// proceed further code
}
else
{
// code 
}

      

Here the label contains "No match", then it is moved to another part that is right. But when the label contains "Timeout" then it gets inside the if loop. So I modified the code like this

If((!label.Text.Contain("No match")) || (!label.Text.Contain("Time out")))
{
// proceed further code
}
else
{
// code 
}

      

still not working. If the label contains "Timeout" it still goes into the loop else else.Label contains only one text at a time, either "No match" or "Timeout" or any other text.

+3


source to share


3 answers


I suspect you want:

if(!(label.Text.Contains("No match") || label.Text.Contains("Time out")))
{
    // proceed further code
}
else
{
   // code 
}

      

Pay attention to bracketing. Inner part

label.Text.Contains("No match") || label.Text.Contains("Time out")

      

and then inverted. I would probably pull this into a separate variable:



bool timedOutOrNoMatch = label.Text.Contains("No match") || 
                         label.Text.Contains("Time out");
if (!timedOutOrNoMatch)
{
}
else 
{
}

      

Alternatively, invert its meaning:

if (label.Text.Contains("No match") || label.Text.Contains("Time out"))
{
    // Do whatever was in your else block.
}
else
{
    // Do whatever was in your first block.
}

      

If your answer to "bad" labels is something that allows you to go back or throw an exception, this can also reduce the amount of nesting:

if (label.Text.Contains("No match") || label.Text.Contains("Time out"))
{
    output.Text = "Go away";
    return;
}
// Now handle the success case

      

+5


source


Try with the following code:

    if(!(label.Text.Contains("No match") || label.Text.Contains("Time out")))
    {
    // proceed further code
    }
    else
    {
    // code 
    }

      



If you want to get right to your modified code, use the AND operator:

       if(!label.Text.Contains("No match") && !label.Text.Contains("Time out"))
        {
        // proceed further code
        }
        else
        {
        // code 
        }

      

+2


source


To write your code in a more readable manner, you must write it in a way that is readable and understandable. I prefer to write this statement like this

bool ProceedFurther()
{
  //Don't proceed if No Match
  if(!label.Text.Contains("No match")) return false;

  //Don't proceed if Time out
  if(!label.Text.Contains("Time out")) return false;

  //Proceed otherwise
  return true;
}

      

and call the Continue method where you want.

If you really only want this operator, then the best is (basically people forget to change || to && after they change the condition to negative (using!).

if(!label.Text.Contains("No match") && !label.Text.Contains("Time out")) 

      

+2


source







All Articles