为什么这不能作为IIF功能,而是作为IF声明? [英] Why won't this work as an IIF function, but will as an IF Statement?

查看:191
本文介绍了为什么这不能作为IIF功能,而是作为IF声明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下作品:

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:


  1. A Boolean 要检查的条件

  2. 如果条件 True 对象 $ c>

  3. 如果条件为 False,则返回不同的对象

  1. A Boolean condition to check
  2. An Object to return if the condition is True
  3. A different Object to return if the condition is False

在您的使用中,您的条件是 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆