VB.NET - IIF(,,) - “双方"被评估.我应该注意哪些情况? [英] VB.NET - IIF(,,) - Both "sides" are evaluated. What situations should I watch out for?

查看:27
本文介绍了VB.NET - IIF(,,) - “双方"被评估.我应该注意哪些情况?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近了解了 IIF(A,B,C) 函数.我是 VB/VB.NET 的长期编码员,最近花了很多时间来加快 SQL 编码的速度.

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.

在 SQL 中一个(明显的)常见的事情是这样的:

One (obvious) common thing to do in SQL is something like the following:

select (case where @var = 0 then MyTable.Val1 else MyTable.Val2 end) from MyTable

IIF(A,B,C) 将允许我在 VB.NET 中做到这一点......所有这些都在一行上.

IIF(A,B,C) will allow me to do this in VB.NET... all on one line.

但是,我已经读到,无论 A 的计算结果如何,B 和 C 都会被计算.

However, I have read that both B and C are evaluated no matter what A evaluates to.

我可以想到一些明显的情况,其中这是一件坏事,例如:

I can think of some obvious situations where this is a bad thing such as:

Dim X as integer = IIF(SomeBoolean = true, ExpensiveFunction1(), ExpensiveFunction2())

由于我将把它包含在我的曲目中,是否有任何更微妙的情况会导致我在使用 IIF 时遇到麻烦?

As I will be including this in my repertoire, are there any more subtle situations where I may get myself into trouble using 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.

在过去几年中,存在一个新的 VB.NET 功能,无需使用 IIF() 函数.

For the past few years, a new VB.NET feature exists which eliminates the need to use the IIF() function.

if(Something = true, ExecuteA(), ExecuteB())

仅执行 ExecuteA() 或 ExecuteB().最后,使用短路内联 IF.

Only ExecuteA() OR ExecuteB() are executed. Finally, inline IF with shortcircuiting.

因此,如果您使用的是 VB.NET 的更高版本(截至 2016 年),请尽可能使用它.

So, if you are using later versions of VB.NET (as of 2016), use this instead if you can.

推荐答案

这是最常见的问题.

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.

另一个可能的逻辑错误是当 iif 的一侧或另一侧调用修改系统状态或具有输出参数的方法时.

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.

Z = iif(FunctionA(InputOutputParam), FunctionB(InputOutputParam))
'InputOutputParam is indeterminate or at least ambiguous here.

根据我的经验,确实没有充分的理由使用 IIF.大多数情况下,它只是用来缩写代码,考虑到它可能导致的问题,它只是不值得.另外,我认为这会使代码更难阅读.

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.

另一件事是它返回一个装箱值(即对象数据类型),您必须将其转换回所需的类型.

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.

这篇关于VB.NET - IIF(,,) - “双方"被评估.我应该注意哪些情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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