0

I am pretty sure that in most languages you can secure the evaluation of a condition by doing one of the following:

if FALSE and (1/0 = 0) {...}

or

if TRUE or (1/0 = 0) {...}

Those examples should not throw exeptions (maybe the second one depending on internal implementation of the evaluation) as the whole if-condition is met even without checking the second condition.

In VBA however both these protections wont work:

If False and t() Then ...
If True or t() Then ...

Function t() As Boolean
MsgBox "This is displayed both times"
End Function

Why does VBA behave this way and what is the best (+shortest) way to do these kind of protections instead?

Example: If s = "" or thisFunctionCantTakeEmptyStrings(s) Then ...

Community
  • 1
  • 1
omnesia
  • 1,992
  • 1
  • 15
  • 14
  • 2
    This already has been asked, it's called short-circuit. Refer to https://stackoverflow.com/questions/24641923/vba-short-circuit-and-alternatives – Michał Turczyn Jul 27 '17 at 08:51
  • Ah ok I did not know the name of this, and therefore did not find much on this. Thank your for the reference – omnesia Jul 27 '17 at 08:53

0 Answers0