为什么这不能作为IIF功能,而是作为IF声明? [英] Why won't this work as an IIF function, but will as an IF Statement?
问题描述
以下作品:
If 1=1
rdoYes.checked = True
Else
rdoNo.checked = True
End If
但是,以下内容不是工作:
However, the following doesn't work:
IIF(1=1, rdoYes.checked = True, rdoNo.checked = True)
这是为什么?
谢谢!
推荐答案
它确实有效;它只是不能做你想要的。
It does "work"; it just doesn't do what you want.
IIf
在VB.NET中是一个函数(不要使用它,永远强大的>,顺便说一下),它采用以下参数:
IIf
in VB.NET is a function (don't use it, ever, by the way), which takes these parameters:
- A
Boolean
要检查的条件 - 如果条件
True $ c则返回
对象
$ c> - 如果条件为
False,则返回不同的
对象
- A
Boolean
condition to check - An
Object
to return if the condition isTrue
- A different
Object
to return if the condition isFalse
在您的使用中,您的条件是 1 = 1
;那么你的另外两个参数是 rdoYes.Checked = True
和 rdoNo.Checked = True
,两者都是 VB
来自VB编译器的表达式(所以,实际上,它们等同于更简单的 rdoYes.Checked
和 rdoNo.Checked
)。
In your usage, your condition is 1 = 1
; then your two other parameters are rdoYes.Checked = True
and rdoNo.Checked = True
, both Boolean
expressions from the VB compiler's perspective (so, really, they're equivalent to the simpler rdoYes.Checked
and rdoNo.Checked
).
请记住,在VB.NET中, =
标志仅分配如果它在自己的行。这就是编译器如何区分语句,如 x = 5
和如果x = 5那么
。
Remember that in VB.NET, the =
sign is only an assignment if it is on its own line. This is how the compiler distinguishes between statements such as x = 5
and If x = 5 Then
.
这与您的问题没有直接关系,但您也应该知道 IIf
已被弃用,您几乎应该总是青睐如果
而不是:
This is not directly related to your question, but you should also be aware that IIf
is deprecated and you should almost always favor If
instead:
' Let us just suppose it made sense to write this: '
' Notice the If instead of IIf. '
Dim result = If(1 = 1, rdoYes.Checked, rdoNo.Checked)
这篇关于为什么这不能作为IIF功能,而是作为IF声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!