Sub Button1_Click
Dim operation As String = "sum"
Dim a() As Int = Array As Int(1,2,3,4)
'==============================================================
' Conditional assignment: the TrueValue and FalseValue parameters are expressions or results of methods that do return values
'==============================================================
' This works because the result of IIf is assigned to 'res'.
Dim res As Int = IIf(operation = "sum", increase(10), decrease(10) )
' This does not compile because the result of IIf is not finally assigned to anything.
IIf(operation = "sum", increase(10), decrease(10) )
'==============================================================
' Conditional execution. The TrueValue and FalseValue parameters are results of methods that do not necessarily return values
'==============================================================
' This does work because increaseAll() and decreaseAll() don't return anything (I guess internally is Null), and the final result (Null) is assigned to an Object
Dim res2 As Object = IIf(operation = "sum", increaseAll(a), decreaseAll(a) )
' This does not compile because the IIf result is not assigned to anything
IIf(operation = "sum", increaseAll(a), decreaseAll(a) )
' BUT, this is accepted, I guess because Subs that return values are accepted to be called without assignment.
PseudoIIf(operation = "sum", increaseAll(a), decreaseAll(a))
End Sub
Sub PseudoIIf (Condition As Boolean, TrueValue As Object, FalseValue As Object) As Object
If Condition = True Then Return TrueValue Else Return FalseValue
End Sub
Sub increase(val as int) as int
return val+1
End Sub
Sub decrease(val as Int) As Int
return val-1
End Sub
Sub increaseAll(vals() As Int)
For k=0 To vals.Length-1
vals(k)=vals(k)+1
Next
End Sub
Sub decreaseAll(vals() As Int)
For k=0 To vals.Length-1
vals(k)=vals(k)-1
Next
End Sub