Evaluating two IF conditions with AND operator

How is it that if I have a condition for mail.Sender IsNot Nothing, the robot jumps to the “Else” section but if I add a another condition - AND mail.Sender.ToString.Contains(sender), I get an exception? Am I supposed to use two IF statements? Sounds very disappointing… If the first condition is false and we’re using AND… he should drop immediately to the “Else” section.

What do you think? Is there a more elegant solution to this than having two IF

image

Thanks in advance.

In my experience, if it is the case that mail.Sender is Nothing it will also try to evaluate the mail.Sender.ToString.Contains(sender) and this will throw the Null Reference Error because mail.Sender does not exist. It checks all the if cases regardless of one being wrong when used with an AND operator.

Unfortunately I’ve only really found this to be a solution:

if(mail.Sender isNot Nothing){
    if(mail.Sender.ToString.Contains(sender)){
        //Activities here
    }else{
       //Handle exception
    }
}else{
    //Handle exception
}

To keep it visually clear I’ve started to use Switch Activities under the boolean evaluation so I don’t have too many nested If Activities that get messy very quickly.

1 Like

@Francisco.Simoes
@mike.vansickle
You can use the short-circuiting operators ANDALSO and ORELSE.

In the following situations the code will only check condition one since the second one won’t matter.
false ANDALSO …
true ORELSE …

3 Likes

This is great, I’m clearly not very experienced with VB.NET, I had no idea these operators existed. Thanks for sharing this!

1 Like

Amazing solution, much like @mike.vansickle I’m not too experienced in .NET. Thank you very much for your help!

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.