[program-l] Re: VB.NET AndAlso vs And

  • From: "David Lant" <david.lant1@xxxxxxxxxxxxxx>
  • To: <program-l@xxxxxxxxxxxxx>
  • Date: Mon, 28 Mar 2005 17:40:23 +0100

That sounds like a reasonable option, if you pardon the pun. <smile>  The
problem with the AndAlso syntax, while minor, is that it makes conditions
more verbose for us screen reader users.  And if you then include the OrElse
operator in a complex condition, it could get pretty wordy.  

If Age > 65 AndAlso blnFemale AndAlso (blnHusbandRetired OrElse blnWidowed)
Then
...
End If

I also feel I may get confused with if statements that include Else clauses,
and so on.  I'm getting too old for all this new fangled malarkey. Lol j/k 

All the best,

David


-----Original Message-----
From: program-l-bounce@xxxxxxxxxxxxx [mailto:program-l-bounce@xxxxxxxxxxxxx]
On Behalf Of Jamal Mazrui
Sent: 28 March 2005 05:33
To: program-l@xxxxxxxxxxxxx
Subject: [program-l] Re: VB.NET AndAlso vs And


Thanks for the clarification, David--I get it now.  

Since VBNET was a substantive rewrite of VB6, I might have instead
implemented shortcircuiting with the cleaner "And" syntax combined with a
compiler directive, something like "Option ShortCircuit."  That would be on
the checklist when porting code to VBNet.  Just my tupence on the matter.
<grin>

Jamal


-----Original Message-----
From: program-l-bounce@xxxxxxxxxxxxx [mailto:program-l-bounce@xxxxxxxxxxxxx]
On Behalf Of David Lant
Sent: Monday, March 28, 2005 11:19 AM
To: program-l@xxxxxxxxxxxxx
Subject: [program-l] Re: VB.NET AndAlso vs And


Hi Jamal,

There may be other things going on in there.  But simply put, you might want
to have both records locked regardless of the result of this particular If
statement.  The fact that you can't lock the first record, doesn't mean you
wouldn't want the second one locked.  It just means that in this particular
If statement it was irrelevant whether it was or not.  But the actual
locking process could still go ahead anyway in those functions.

I, and I'm sure mark, certainly wouldn't recommend doing this, as it's both
obscure and confusing.  But the fact that it's possible means it has to be
backwardly supported I guess.

If ever anything is not conditional, I would never include it in a condition
that implied it was.  E.g.  I would do the following instead of Mark's
example:

blnRecord1Locked = CheckAndHold(rec1)
blnRecord2Locked = CheckAndHold(rec2)
If blnRecord1Locked And blnRecord2Locked then
...
End If

And obviously, with the new VB.Net syntax, it would be:

If blnRecord1Locked AndAlso blnRecord2Locked Then
...
End If

And so, later in the code, you could independently perform processing on
blnRecord2Locked, irrespective of whether the first record was successfully
locked or not.

All the best,

David


-----Original Message-----
From: program-l-bounce@xxxxxxxxxxxxx [mailto:program-l-bounce@xxxxxxxxxxxxx]
On Behalf Of Jamal Mazrui
Sent: 28 March 2005 05:08
To: program-l@xxxxxxxxxxxxx
Subject: [program-l] Re: VB.NET AndAlso vs And


Hi Mark,
I'm not understanding your example.  If an attempt to lock the first record
failed, why would one want to attempt a lock on the second one?

Jamal


-----Original Message-----
From: program-l-bounce@xxxxxxxxxxxxx [mailto:program-l-bounce@xxxxxxxxxxxxx]
On Behalf Of David Lant
Sent: Monday, March 28, 2005 10:54 AM
To: program-l@xxxxxxxxxxxxx
Subject: [program-l] Re: VB.NET AndAlso vs And


Hi Mark,

Ah, now I see.  *thumps head*  Of course.  The expression may actually
execute other code, rather than simply cause a result to be returned,
therefore someone could have converted a VB6 program which does this, hence
the need to retain the original And interpretation.

Well, I'll certainly be being more careful about this in future. <grin>

All the best,

David


-----Original Message-----
From: program-l-bounce@xxxxxxxxxxxxx [mailto:program-l-bounce@xxxxxxxxxxxxx]
On Behalf Of Mark Long
Sent: 28 March 2005 04:42
To: program-l@xxxxxxxxxxxxx; program-l@xxxxxxxxxxxxx
Subject: [program-l] Re: VB.NET AndAlso vs And


To answer David's question, you would want to avoid the short circuit when
you were expecting side-effects for functions called in the evaluation. So,
to give some air code
 
If CheckRecordAndTryToLock(rec) and CheckRecordAndTryToLock(rec2) then
 
  ' we have both locks...
 
HTH Mark

________________________________

From: program-l-bounce@xxxxxxxxxxxxx on behalf of tyler
Sent: Mon 28/03/2005 14:34
To: program-l@xxxxxxxxxxxxx
Subject: [program-l] Re: VB.NET AndAlso vs And



 *pout* I am getting way better, and have used the online documentation a
lot. Ty Littlefield
email:
compgeek13@xxxxxxx
----- Original Message -----
From: "David Lant" <david.lant1@xxxxxxxxxxxxxx>
To: <program-l@xxxxxxxxxxxxx>
Sent: Monday, March 28, 2005 6:32 AM
Subject: [program-l] VB.NET AndAlso vs And


Hi all,

I was reading through an interesting article on MSDN about semi-connected
applications using VB.NET, and came across the AndAlso logical operator. As
I'd not noticed it before, I looked it up in the online help.  (Tyler, take
note.)

I fully understand that using the AndAlso operator shortcuts the evaluation
of each expression, so that if any of the component expressions evaluate to
False, then the remaining expressions are not evaluated.  The problem is, I
can't for the life of me understand why one would *not* want to shortcut
such logical evaluations.

I do appreciate that the And operator has an additional function of
performing bitwise And operation on expressions.  But I'm left thinking that
what this really means is that under VB.NET, we should always use AndAlso
for logical conjunctions on expressions, and only use And for bitwise
comparisons.

Is this right?  Or am I leaping to an unfounded conclusion?  If anyone can
offer a reason why you would want to evaluate something that wouldn't affect
the result, I would very much appreciate it. <smile>

All the best,

David


** To leave the list, click on the immediately-following link:-
** [mailto:program-l-request@xxxxxxxxxxxxx?subject=unsubscribe]
** If this link doesn't work then send a message to:
** program-l-request@xxxxxxxxxxxxx
** and in the Subject line type
** unsubscribe
** For other list commands such as vacation mode, click on the
** immediately-following link:-
** [mailto:program-l-request@xxxxxxxxxxxxx?subject=faq]
** or send a message, to
** program-l-request@xxxxxxxxxxxxx with the Subject:- faq


** To leave the list, click on the immediately-following link:-
** [mailto:program-l-request@xxxxxxxxxxxxx?subject=unsubscribe]
** If this link doesn't work then send a message to:
** program-l-request@xxxxxxxxxxxxx
** and in the Subject line type
** unsubscribe
** For other list commands such as vacation mode, click on the
** immediately-following link:-
** [mailto:program-l-request@xxxxxxxxxxxxx?subject=faq]
** or send a message, to
** program-l-request@xxxxxxxxxxxxx with the Subject:- faq

** To leave the list, click on the immediately-following link:-
** [mailto:program-l-request@xxxxxxxxxxxxx?subject=unsubscribe]
** If this link doesn't work then send a message to:
** program-l-request@xxxxxxxxxxxxx
** and in the Subject line type
** unsubscribe
** For other list commands such as vacation mode, click on the
** immediately-following link:-
** [mailto:program-l-request@xxxxxxxxxxxxx?subject=faq]
** or send a message, to
** program-l-request@xxxxxxxxxxxxx with the Subject:- faq

** To leave the list, click on the immediately-following link:-
** [mailto:program-l-request@xxxxxxxxxxxxx?subject=unsubscribe]
** If this link doesn't work then send a message to:
** program-l-request@xxxxxxxxxxxxx
** and in the Subject line type
** unsubscribe
** For other list commands such as vacation mode, click on the
** immediately-following link:-
** [mailto:program-l-request@xxxxxxxxxxxxx?subject=faq]
** or send a message, to
** program-l-request@xxxxxxxxxxxxx with the Subject:- faq

** To leave the list, click on the immediately-following link:-
** [mailto:program-l-request@xxxxxxxxxxxxx?subject=unsubscribe]
** If this link doesn't work then send a message to:
** program-l-request@xxxxxxxxxxxxx
** and in the Subject line type
** unsubscribe
** For other list commands such as vacation mode, click on the
** immediately-following link:-
** [mailto:program-l-request@xxxxxxxxxxxxx?subject=faq]
** or send a message, to
** program-l-request@xxxxxxxxxxxxx with the Subject:- faq

** To leave the list, click on the immediately-following link:-
** [mailto:program-l-request@xxxxxxxxxxxxx?subject=unsubscribe]
** If this link doesn't work then send a message to:
** program-l-request@xxxxxxxxxxxxx
** and in the Subject line type
** unsubscribe
** For other list commands such as vacation mode, click on the
** immediately-following link:-
** [mailto:program-l-request@xxxxxxxxxxxxx?subject=faq]
** or send a message, to
** program-l-request@xxxxxxxxxxxxx with the Subject:- faq

** To leave the list, click on the immediately-following link:-
** [mailto:program-l-request@xxxxxxxxxxxxx?subject=unsubscribe]
** If this link doesn't work then send a message to:
** program-l-request@xxxxxxxxxxxxx
** and in the Subject line type
** unsubscribe
** For other list commands such as vacation mode, click on the
** immediately-following link:-
** [mailto:program-l-request@xxxxxxxxxxxxx?subject=faq]
** or send a message, to
** program-l-request@xxxxxxxxxxxxx with the Subject:- faq

Other related posts: