In FreeBasic, IIf (A, B, C) is equivalent to A ? B : C in C/C++/Java/C#, If (A, B, C) in VB.NET, and B if A else C in python. However, in VB6, Office VBA and VB.NET, IIf (A, B, C) is equivalent to this C++ code:
bool a = A;
auto b = B;
auto c = C;
a ? b : c;
As a result, IIf works differently in Visual Basic and FreeBasic. For example,
Dim x As Integer, y As Integer
x = 0
y = IIf (x <> 0, 12 \ x, 0)
works normally in FreeBasic, but will throw an exception in Visual Basic. That is because in Visual Basic this code is equivalent to the following C++ code:
int x, y;
x = 0;
bool a = (x != 0);
auto b = 12 / x;
auto c = 0;
y = a ? b : c;
In VB.NET, using If will do the same thing as using IIf in FreeBasic. For example,
Dim x As Integer, y As Integer
x = 0
y = If (x <> 0, 12 \ x, 0)
in VB.NET will not throw exception.
The question is: Why does IIf (A, B, C) in VB6/VBA evaluate both B and C, not selecting one expression to evaluate depending on the value of expression A?