假设totallvl是一个整数并且chclass1已创建但chclass2尚未创建,为什么我可以这样做:

totallvl = chclass1.level
If chclass2 IsNot Nothing Then
    totallvl = totallvl + chclass2.level
End If

但是不是吗?
totallvl = chclass1.level + IIf(chclass2 Is Nothing, 0, chclass2.level)

就像编译器假设在此示例中使用chclass2,但在第一个示例中不使用。

最佳答案

IIf只是一个函数;不管第一个参数是什么,chclass2.level都会被求值。如果希望使用类似于其他语言的内联条件运算符,请使用实际的内联If(在VB 2008及更高版本中可用):

If(chclass2 Is Nothing, 0, chclass2.level)

关于vb.net - 在表达式中使用IIf,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14421205/

10-16 18:35