AND/AndAlso with Or/OrElse confusion












-1















I need to ensure the first item has a entry. (This is an import of a CSV file).
The intended action is if Item(471)).ToString is blank but there is a value >0 in either Item(475)).ToString) or Item(479)).ToString) alert that a date is required.



I have tried a few ways and cannot get this to work. If I take the OrElse statement out and use only one of the two it works. I am confused the proper syntax to get this to work. Can someone please point me in the correct direction?



    If (MyList.Items.Item(471)).ToString = "" And CDec((MyList.Items.Item(475)).ToString) > 0D OrElse CDec((MyList.Items.Item(479)).ToString) > 0D Then
MessageBox.Show("FUELING DATE #1 CANNOT BE BLANK", "Critical Warning", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Else
tbDate1.Text = (MyList.Items.Item(471)).ToString
End If









share|improve this question























  • What's the confusion, AndAlso / OrElse 'short circuit'? IF 1 = 0 And 1 = 1 will evaluate both conditions, even though it knows the first condition isn't true. In cases where a function is in your if-line, this can lead to unexpected results. IF 1 = 0 AndAlso 1 = 1" checks 1 = 0, evaluates to false, and ditches the if-branch. Likewise with Or, which evaluates all conditions, even if first succeeds. OrElse continues forward only if it still needs to find a match. You might do If ToyInBox("truck") OrElse ToyInPocket("truck"), ToyInPocket() is only checked if ToyInBox() is false

    – Regular Joe
    Nov 13 '18 at 21:05













  • If you're confused, it's because you didn't listen well enough in maths class when taught about operator precedence. In Boolean logic, AND takes precedence over OR so you must use parentheses if you want an OR operator evaluated before an AND operator. It's exactly the same situation us using parentheses to for an addition to be evaluated before a multiplication in a basic arithmetic expression.

    – jmcilhinney
    Nov 14 '18 at 0:12






  • 1





    As for using choosing between And/Or and AndAlso/OrElse, that's irrelevant to this question. Either would work in this particular case but you should always work by the following rule: ALWAYS use AndAlso/OrElse unless you specifically don't want short-circuiting and, if you don't want short-circuiting then you probably ought to rethink your design to avoid it.

    – jmcilhinney
    Nov 14 '18 at 0:14


















-1















I need to ensure the first item has a entry. (This is an import of a CSV file).
The intended action is if Item(471)).ToString is blank but there is a value >0 in either Item(475)).ToString) or Item(479)).ToString) alert that a date is required.



I have tried a few ways and cannot get this to work. If I take the OrElse statement out and use only one of the two it works. I am confused the proper syntax to get this to work. Can someone please point me in the correct direction?



    If (MyList.Items.Item(471)).ToString = "" And CDec((MyList.Items.Item(475)).ToString) > 0D OrElse CDec((MyList.Items.Item(479)).ToString) > 0D Then
MessageBox.Show("FUELING DATE #1 CANNOT BE BLANK", "Critical Warning", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Else
tbDate1.Text = (MyList.Items.Item(471)).ToString
End If









share|improve this question























  • What's the confusion, AndAlso / OrElse 'short circuit'? IF 1 = 0 And 1 = 1 will evaluate both conditions, even though it knows the first condition isn't true. In cases where a function is in your if-line, this can lead to unexpected results. IF 1 = 0 AndAlso 1 = 1" checks 1 = 0, evaluates to false, and ditches the if-branch. Likewise with Or, which evaluates all conditions, even if first succeeds. OrElse continues forward only if it still needs to find a match. You might do If ToyInBox("truck") OrElse ToyInPocket("truck"), ToyInPocket() is only checked if ToyInBox() is false

    – Regular Joe
    Nov 13 '18 at 21:05













  • If you're confused, it's because you didn't listen well enough in maths class when taught about operator precedence. In Boolean logic, AND takes precedence over OR so you must use parentheses if you want an OR operator evaluated before an AND operator. It's exactly the same situation us using parentheses to for an addition to be evaluated before a multiplication in a basic arithmetic expression.

    – jmcilhinney
    Nov 14 '18 at 0:12






  • 1





    As for using choosing between And/Or and AndAlso/OrElse, that's irrelevant to this question. Either would work in this particular case but you should always work by the following rule: ALWAYS use AndAlso/OrElse unless you specifically don't want short-circuiting and, if you don't want short-circuiting then you probably ought to rethink your design to avoid it.

    – jmcilhinney
    Nov 14 '18 at 0:14
















-1












-1








-1








I need to ensure the first item has a entry. (This is an import of a CSV file).
The intended action is if Item(471)).ToString is blank but there is a value >0 in either Item(475)).ToString) or Item(479)).ToString) alert that a date is required.



I have tried a few ways and cannot get this to work. If I take the OrElse statement out and use only one of the two it works. I am confused the proper syntax to get this to work. Can someone please point me in the correct direction?



    If (MyList.Items.Item(471)).ToString = "" And CDec((MyList.Items.Item(475)).ToString) > 0D OrElse CDec((MyList.Items.Item(479)).ToString) > 0D Then
MessageBox.Show("FUELING DATE #1 CANNOT BE BLANK", "Critical Warning", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Else
tbDate1.Text = (MyList.Items.Item(471)).ToString
End If









share|improve this question














I need to ensure the first item has a entry. (This is an import of a CSV file).
The intended action is if Item(471)).ToString is blank but there is a value >0 in either Item(475)).ToString) or Item(479)).ToString) alert that a date is required.



I have tried a few ways and cannot get this to work. If I take the OrElse statement out and use only one of the two it works. I am confused the proper syntax to get this to work. Can someone please point me in the correct direction?



    If (MyList.Items.Item(471)).ToString = "" And CDec((MyList.Items.Item(475)).ToString) > 0D OrElse CDec((MyList.Items.Item(479)).ToString) > 0D Then
MessageBox.Show("FUELING DATE #1 CANNOT BE BLANK", "Critical Warning", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Else
tbDate1.Text = (MyList.Items.Item(471)).ToString
End If






vb.net






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 20:49









John EtlingJohn Etling

17




17













  • What's the confusion, AndAlso / OrElse 'short circuit'? IF 1 = 0 And 1 = 1 will evaluate both conditions, even though it knows the first condition isn't true. In cases where a function is in your if-line, this can lead to unexpected results. IF 1 = 0 AndAlso 1 = 1" checks 1 = 0, evaluates to false, and ditches the if-branch. Likewise with Or, which evaluates all conditions, even if first succeeds. OrElse continues forward only if it still needs to find a match. You might do If ToyInBox("truck") OrElse ToyInPocket("truck"), ToyInPocket() is only checked if ToyInBox() is false

    – Regular Joe
    Nov 13 '18 at 21:05













  • If you're confused, it's because you didn't listen well enough in maths class when taught about operator precedence. In Boolean logic, AND takes precedence over OR so you must use parentheses if you want an OR operator evaluated before an AND operator. It's exactly the same situation us using parentheses to for an addition to be evaluated before a multiplication in a basic arithmetic expression.

    – jmcilhinney
    Nov 14 '18 at 0:12






  • 1





    As for using choosing between And/Or and AndAlso/OrElse, that's irrelevant to this question. Either would work in this particular case but you should always work by the following rule: ALWAYS use AndAlso/OrElse unless you specifically don't want short-circuiting and, if you don't want short-circuiting then you probably ought to rethink your design to avoid it.

    – jmcilhinney
    Nov 14 '18 at 0:14





















  • What's the confusion, AndAlso / OrElse 'short circuit'? IF 1 = 0 And 1 = 1 will evaluate both conditions, even though it knows the first condition isn't true. In cases where a function is in your if-line, this can lead to unexpected results. IF 1 = 0 AndAlso 1 = 1" checks 1 = 0, evaluates to false, and ditches the if-branch. Likewise with Or, which evaluates all conditions, even if first succeeds. OrElse continues forward only if it still needs to find a match. You might do If ToyInBox("truck") OrElse ToyInPocket("truck"), ToyInPocket() is only checked if ToyInBox() is false

    – Regular Joe
    Nov 13 '18 at 21:05













  • If you're confused, it's because you didn't listen well enough in maths class when taught about operator precedence. In Boolean logic, AND takes precedence over OR so you must use parentheses if you want an OR operator evaluated before an AND operator. It's exactly the same situation us using parentheses to for an addition to be evaluated before a multiplication in a basic arithmetic expression.

    – jmcilhinney
    Nov 14 '18 at 0:12






  • 1





    As for using choosing between And/Or and AndAlso/OrElse, that's irrelevant to this question. Either would work in this particular case but you should always work by the following rule: ALWAYS use AndAlso/OrElse unless you specifically don't want short-circuiting and, if you don't want short-circuiting then you probably ought to rethink your design to avoid it.

    – jmcilhinney
    Nov 14 '18 at 0:14



















What's the confusion, AndAlso / OrElse 'short circuit'? IF 1 = 0 And 1 = 1 will evaluate both conditions, even though it knows the first condition isn't true. In cases where a function is in your if-line, this can lead to unexpected results. IF 1 = 0 AndAlso 1 = 1" checks 1 = 0, evaluates to false, and ditches the if-branch. Likewise with Or, which evaluates all conditions, even if first succeeds. OrElse continues forward only if it still needs to find a match. You might do If ToyInBox("truck") OrElse ToyInPocket("truck"), ToyInPocket() is only checked if ToyInBox() is false

– Regular Joe
Nov 13 '18 at 21:05







What's the confusion, AndAlso / OrElse 'short circuit'? IF 1 = 0 And 1 = 1 will evaluate both conditions, even though it knows the first condition isn't true. In cases where a function is in your if-line, this can lead to unexpected results. IF 1 = 0 AndAlso 1 = 1" checks 1 = 0, evaluates to false, and ditches the if-branch. Likewise with Or, which evaluates all conditions, even if first succeeds. OrElse continues forward only if it still needs to find a match. You might do If ToyInBox("truck") OrElse ToyInPocket("truck"), ToyInPocket() is only checked if ToyInBox() is false

– Regular Joe
Nov 13 '18 at 21:05















If you're confused, it's because you didn't listen well enough in maths class when taught about operator precedence. In Boolean logic, AND takes precedence over OR so you must use parentheses if you want an OR operator evaluated before an AND operator. It's exactly the same situation us using parentheses to for an addition to be evaluated before a multiplication in a basic arithmetic expression.

– jmcilhinney
Nov 14 '18 at 0:12





If you're confused, it's because you didn't listen well enough in maths class when taught about operator precedence. In Boolean logic, AND takes precedence over OR so you must use parentheses if you want an OR operator evaluated before an AND operator. It's exactly the same situation us using parentheses to for an addition to be evaluated before a multiplication in a basic arithmetic expression.

– jmcilhinney
Nov 14 '18 at 0:12




1




1





As for using choosing between And/Or and AndAlso/OrElse, that's irrelevant to this question. Either would work in this particular case but you should always work by the following rule: ALWAYS use AndAlso/OrElse unless you specifically don't want short-circuiting and, if you don't want short-circuiting then you probably ought to rethink your design to avoid it.

– jmcilhinney
Nov 14 '18 at 0:14







As for using choosing between And/Or and AndAlso/OrElse, that's irrelevant to this question. Either would work in this particular case but you should always work by the following rule: ALWAYS use AndAlso/OrElse unless you specifically don't want short-circuiting and, if you don't want short-circuiting then you probably ought to rethink your design to avoid it.

– jmcilhinney
Nov 14 '18 at 0:14














1 Answer
1






active

oldest

votes


















-1














If (MyList.Items.Item(471)).ToString = "" AndAlso (CDec((MyList.Items.Item(475)).ToString) > 0D OrElse CDec((MyList.Items.Item(479)).ToString) > 0D) Then
MessageBox.Show("FUELING DATE #1 CANNOT BE BLANK", "Critical Warning", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Else
tbDate1.Text = (MyList.Items.Item(471)).ToString
End If


In short



If condition1 AndAlso (condition2 OrElse condition3) Then





share|improve this answer
























  • My overall error was that I had forgotten I needed Parenthesis around my OrElse statement. Thanks for the help.

    – John Etling
    Nov 15 '18 at 6:28











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53289279%2fand-andalso-with-or-orelse-confusion%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









-1














If (MyList.Items.Item(471)).ToString = "" AndAlso (CDec((MyList.Items.Item(475)).ToString) > 0D OrElse CDec((MyList.Items.Item(479)).ToString) > 0D) Then
MessageBox.Show("FUELING DATE #1 CANNOT BE BLANK", "Critical Warning", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Else
tbDate1.Text = (MyList.Items.Item(471)).ToString
End If


In short



If condition1 AndAlso (condition2 OrElse condition3) Then





share|improve this answer
























  • My overall error was that I had forgotten I needed Parenthesis around my OrElse statement. Thanks for the help.

    – John Etling
    Nov 15 '18 at 6:28
















-1














If (MyList.Items.Item(471)).ToString = "" AndAlso (CDec((MyList.Items.Item(475)).ToString) > 0D OrElse CDec((MyList.Items.Item(479)).ToString) > 0D) Then
MessageBox.Show("FUELING DATE #1 CANNOT BE BLANK", "Critical Warning", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Else
tbDate1.Text = (MyList.Items.Item(471)).ToString
End If


In short



If condition1 AndAlso (condition2 OrElse condition3) Then





share|improve this answer
























  • My overall error was that I had forgotten I needed Parenthesis around my OrElse statement. Thanks for the help.

    – John Etling
    Nov 15 '18 at 6:28














-1












-1








-1







If (MyList.Items.Item(471)).ToString = "" AndAlso (CDec((MyList.Items.Item(475)).ToString) > 0D OrElse CDec((MyList.Items.Item(479)).ToString) > 0D) Then
MessageBox.Show("FUELING DATE #1 CANNOT BE BLANK", "Critical Warning", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Else
tbDate1.Text = (MyList.Items.Item(471)).ToString
End If


In short



If condition1 AndAlso (condition2 OrElse condition3) Then





share|improve this answer













If (MyList.Items.Item(471)).ToString = "" AndAlso (CDec((MyList.Items.Item(475)).ToString) > 0D OrElse CDec((MyList.Items.Item(479)).ToString) > 0D) Then
MessageBox.Show("FUELING DATE #1 CANNOT BE BLANK", "Critical Warning", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Else
tbDate1.Text = (MyList.Items.Item(471)).ToString
End If


In short



If condition1 AndAlso (condition2 OrElse condition3) Then






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 '18 at 20:54









Martin S.Martin S.

666




666













  • My overall error was that I had forgotten I needed Parenthesis around my OrElse statement. Thanks for the help.

    – John Etling
    Nov 15 '18 at 6:28



















  • My overall error was that I had forgotten I needed Parenthesis around my OrElse statement. Thanks for the help.

    – John Etling
    Nov 15 '18 at 6:28

















My overall error was that I had forgotten I needed Parenthesis around my OrElse statement. Thanks for the help.

– John Etling
Nov 15 '18 at 6:28





My overall error was that I had forgotten I needed Parenthesis around my OrElse statement. Thanks for the help.

– John Etling
Nov 15 '18 at 6:28


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53289279%2fand-andalso-with-or-orelse-confusion%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

The Sandy Post

Danny Elfman

Pages that link to "Head v. Amoskeag Manufacturing Co."