VB.NET - IIF(,,) - 评估“双方”。我应该注意哪些情况?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1220411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VB.NET - IIF(,,) - Both "sides" are evaluated. What situations should I watch out for?
提问by Brian Webster
I recently learned of the IIF(A,B,C) function. I'm a long time VB/VB.NET Coder who recently spent a lot of time coming up to speed in SQL coding.
我最近了解了 IIF(A,B,C) 函数。我是 VB/VB.NET 编码员,最近花了很多时间来加快 SQL 编码的速度。
One (obvious) common thing to do in SQL is something like the following:
在 SQL 中要做的一件(显而易见的)常见事情是这样的:
select (case where @var = 0 then MyTable.Val1 else MyTable.Val2 end) from MyTable
IIF(A,B,C) will allow me to do this in VB.NET... all on one line.
IIF(A,B,C) 将允许我在 VB.NET 中做到这一点......所有这些都在一行上。
However, I have read that both B and C are evaluated no matter what A evaluates to.
但是,我已经读过无论 A 评估什么,B 和 C 都会被评估。
I can think of some obvious situations where this is a bad thing such as:
我可以想到一些明显的情况,其中这是一件坏事,例如:
Dim X as integer = IIF(SomeBoolean = true, ExpensiveFunction1(), ExpensiveFunction2())
As I will be including this in my reperttheitroade, are there any more subtle situations where I may get myself into trouble using IIF?
由于我将把它包含在我的曲目中,是否有任何更微妙的情况会导致我在使用 IIF 时遇到麻烦?
It's a pretty big departure in some situations from using the old fashioned:
在某些情况下,使用老式的做法有很大的不同:
Dim X as integer
if SomeBoolean = true then
X = ExpensiveFunction1()
else
X = ExpensiveFunction2()
end if
I'm hoping to save myself some annoying performance issues and/or bugs in the future.
我希望在未来为自己省去一些烦人的性能问题和/或错误。
Update 2016
2016 年更新
For the past few years, a new VB.NET feature exists which eliminates the need to use the IIF() function.
在过去几年中,存在一个新的 VB.NET 功能,它消除了使用 IIF() 函数的需要。
if(Something = true, ExecuteA(), ExecuteB())
Only ExecuteA() OR ExecuteB() are executed. Finally, inline IF with shortcircuiting.
仅执行 ExecuteA() 或 ExecuteB()。最后,内联 IF 与短路。
So, if you are using later versions of VB.NET (as of 2016), use this instead if you can.
因此,如果您使用的是 VB.NET 的更高版本(截至 2016 年),请尽可能使用它。
采纳答案by JohnFx
Here is the most common gotcha.
这是最常见的问题。
Z = iif(y=0, 0, x/y) 'Throws a divide by zero exception when y is 0
Don't use it to avoid division by zero errors.
不要使用它来避免除以零错误。
Another possible logic bug is when one side of the iif or the other calls a method that modifies the system state or has output parameters.
另一个可能的逻辑错误是当 iif 的一侧或另一侧调用修改系统状态或具有输出参数的方法时。
Z = iif(FunctionA(InputOutputParam), FunctionB(InputOutputParam))
'InputOutputParam is indeterminate or at least ambiguous here.
There really is no good reason to use IIF in my experience. Mostly it is just used to abbreviate code and given the problems it can cause, it just isn't worth it. Plus, I think it makes the code harder to read.
根据我的经验,确实没有充分的理由使用 IIF。大多数情况下,它只是用来缩写代码,考虑到它可能导致的问题,它只是不值得。另外,我认为它使代码更难阅读。
The other thing that bites is that it returns a boxed value (ie an Object data type) which you have to cast back to the desired type.
另一件事是它返回一个装箱值(即对象数据类型),您必须将其转换回所需的类型。
回答by lavinio
[IIF
, not IFF
]
[ IIF
,不是IFF
]
The most common case we've seen is that one side or the other evaluates to Nothing
.
我们见过的最常见的情况是一侧或另一侧的计算结果为Nothing
。
Your code might be expecting to use IIF
as guard to keep from getting a NullReferenceException
, like this:
您的代码可能希望IIF
用作保护以防止获得NullReferenceException
,如下所示:
IIF(something Is Nothing, "nothing", something.Value)
But that won't work, because both sides are always evaluated. This happens a lotin code written by people who come from a C/C++/C#/Java background, since in those languages the ternary operator ?:
does short-circuit evaluation.
但这行不通,因为双方总是被评估。这发生在一个大量的代码由谁来自C / C ++ / C#/ Java的背景,因为这些语言的三元运算符人写的?:
确实短路评价。
And the fact that the VS 2005 IIF() documentationstates that IIF
is just like ?:
doesn't help:
并且VS 2005 IIF() 文档指出的事实IIF
就像?:
无济于事:
The IIf function provides a counterpart for the ternary Conditional Operator: ? : in Visual C++.
IIf 函数为三元条件运算符提供了一个对应物: ? : 在 Visual C++ 中。
Nowhere on that reference page does it state that both sides are evaluated.
该参考页面上没有任何地方说明双方都经过评估。
回答by Amber
According to MSDN, the "If" operator introduced in VB2008 will short-circuit instead, which would be ideal for your expensive computation case:
根据 MSDN,VB2008 中引入的“If”运算符将改为短路,这对于您昂贵的计算案例来说是理想的:
回答by Cyril Gupta
Well, you should also make sure you don't have any functions in in iif that modifies any data based on the condition. We use If for a rather lot of that. It just pays to remember that about iif.
好吧,您还应该确保 iif 中没有任何根据条件修改任何数据的函数。我们使用 If 来做相当多的事情。记住关于 iif 是值得的。
回答by Kristian
I was forced into using an iif (for compactness of code) where I had a chunk of code that was copying values out of many arrays into a spreadsheet, but "nothing" entries in the array causes the code to exit the subroutine (not crash), so I wrapped the line in an iif to check if the array cell contained nothing - if it did, then pass back "" otherwise, pass back the array cell (converting to a string 1st). So as said above, another reason not to use iif.
我被迫使用 iif(为了代码的紧凑性),其中有一段代码将许多数组中的值复制到电子表格中,但数组中的“无”条目导致代码退出子例程(而不是崩溃) ),所以我用一个 iif 将行包裹起来,以检查数组单元格是否包含任何内容 - 如果包含,则传回 "" 否则,传回数组单元格(转换为第一个字符串)。所以如上所述,另一个不使用 iif 的原因。
I'm still in mourning due to the lack of the NZ function that I used all the time in MS Access.
由于缺少我一直在 MS Access 中使用的 NZ 功能,我仍然在哀悼。