Assuming totallvl is an integer and chclass1 has been created but chclass2 has not, why can I do this:
totallvl = chclass1.level
If chclass2 IsNot Nothing Then
totallvl = totallvl + chclass2.level
End If
but not this?
totallvl = chclass1.level + IIf(chclass2 Is Nothing, 0, chclass2.level)
It’s like the compiler is assuming I’ll use chclass2 in this example but not in the first example.
IIfis just a function;chclass2.levelis evaluated regardless of what the first argument is. If you want an inline conditional operator similar to other languages, use an actual inlineIf(available in VB 2008 and later):