Short version
How does the IF()
function cause the passed expressions to NOT be evaluated prior to being passed in as parameters?
Detailed version
I am part of a standards committee, and a discussion was initiated which, in general, is trying to ban the use of the IIF()
function. The general consensus is that if you want to do a short line-length assignment IF()
is okay. And if you for some reason want to evaluate two expressions, don't try to accomplish that all on one line, explicitly and purposefully evaluate the expressions on seperate lines, then do an IF()
call with the results in the If True
and If Else
parameters.
I'm asserting that before a Function(param1, expression)
is stepped into, any expressions are evaluated. Then, their evaluated value is passed as a parameter.
That being asserted, this makes sense:
Sub Main()
Dim intTest As Integer = 0
Dim blnResult As Boolean = IIf(2 = 2, Integer.TryParse("3", intTest), Integer.TryParse("4", intTest))
'intTest = 4
End Sub
- As expected,
blnResult
isTrue
. - Because the conditional of the
IIF()
function isTrue
, the value of the expression in the"If True"
parameter is the resulting assignment. - But BOTH the
If True
ANDIf Else
expressions were evaluated (in written order) prior to even stepping into theIIF()
function, sointTest
became4
. - As far as I'm concerned, that is programatically expected, but not the results desired.
On the reverse side of the coin, we have this:
Sub Main()
Dim intTest As Integer = 0
Dim blnResult As Boolean = If(2 = 2, Integer.TryParse("3", intTest), Integer.TryParse("4", intTest))
'intTest = 3
End Sub
As the inverse of the previous issue, we now have:
- As expected,
blnResult
is True. - Because the conditional of the
IF()
function isTrue
, the value of the expression in the"If True"
parameter is the resulting assignment. - But ONLY the
If True
(in this case) is evaluated, sointTest
became3
. - As far as I'm concerned, that is the results desired, but not programatically expected.
I'd like to see a custom function that does the same thing as IF()
.
IFF(A,B,C)
is equivalent toA?B:C
in a C-esque language? – Aug 15 '13 at 21:18IFF
is a Mathematical functionality, not to be confused with VB.NET'sIIF
. But C-style:A?B:C
, I believe, is more related toIF(A,B,C)
in VB.NET, because if A is true, B is evaluated and assigned. Else, C is evaluated and assigned. But in C-esque, it makes more sense since that is procedural, whereas in VB.NET, it is a Function call, and that is the center of this question. – Suamere Aug 15 '13 at 21:20IF()
wasn't a regular function but special in this regard, perhaps a keyword which just mandates parentheses to look like a function. This isn't an answer I have no idea how to verify my hypothesis. If this question is about VB.NET and not earlier versions, there may be an ECMA spec somewhere. – Aug 15 '13 at 22:07