Jui Test
Jui Test

Reputation: 2409

If condition's logic is not working

I have added one label in form that is not visible to user.Base on the text that label contain proceed further.

Here is my logic,but it fail.I wanted like this,if label contain "No match" or "Time out",should not proceed.

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

Here Label contain "No match",then it move to else part that is right.But when label contain "Time out",then it go inside the if loop.So I modified code like this

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

still not working.If label contain "Time out",still it go into if loop not else loop.Label contain only one text at a time either "No match" or "Time out" or any other text.

Upvotes: 3

Views: 5645

Answers (3)

Jon Skeet
Jon Skeet

Reputation: 1500015

I suspect you want:

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

Note the bracketing. The inner part is

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

and then that's inverted. I would probably pull that out into a separate variable:

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

Alternatively, invert the sense of it:

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 response to the "bad" labels is something that lets you return 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

Upvotes: 5

Imran Rizvi
Imran Rizvi

Reputation: 7438

To write your code in more understood form , you should write it in a way that it is readable and more 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 ProceedFurther method at desired location.

If you really want only that statement, following is the best (mostly people forget to change || to && after they change the condition to negative (using !).

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

Upvotes: 2

user1082916
user1082916

Reputation:

Try with following code:

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

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

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

Upvotes: 2

Related Questions