VBScript的条件短路的解决方法 [英] VBScript conditional short-circuiting workaround
问题描述
我有一个大的传统的ASP应用程序,我要保持,而且我一再发现自己因缺乏短路计算能力的阻碍。例如,VBScript中不会让你逃脱:
I have a large classic ASP app that I have to maintain, and I repeatedly find myself thwarted by the lack of short-circuit evaluation capability. E.g., VBScript won't let you get away with:
if not isNull(Rs("myField")) and Rs("myField") <> 0 then
...
...因为如果卢比(MyField的)为空,你会得到一个错误的第二个条件,比较空为0。因此,我通常会结束,而不是做这样的:
...because if Rs("myField") is null, you get an error in the second condition, comparing null to 0. So I'll typically end up doing this instead:
dim myField
if isNull(Rs("myField")) then
myField = 0
else
myField = Rs("myField")
end if
if myField <> 0 then
...
显然,冗长是pretty惨不忍睹。环顾这个大code碱基,我已经找到了最好的解决方法是使用一个函数原来的程序员写的,叫TernaryOp,基本移植物三元运算符一样的功能,但使用的是临时变量我仍然停留这不会更全功能的语言是必要的。有没有更好的办法?一些超级秘密的方式,短路确实在VBScript存在?
Obviously, the verboseness is pretty appalling. Looking around this large code base, the best workaround I've found is to use a function the original programmer wrote, called TernaryOp, which basically grafts in ternary operator-like functionality, but I'm still stuck using a temporary variable that would not be necessary in a more full-featured language. Is there a better way? Some super-secret way that short-circuiting really does exist in VBScript?
推荐答案
也许不是最好的方式,但它肯定的作品...此外,如果你在VB6或.NET,你可以有强制转换为适当的方法不同键入了。
Maybe not the best way, but it certainly works... Also, if you are in vb6 or .net, you can have different methods that cast to proper type too.
if cint( getVal( rs("blah"), "" ) )<> 0 then
'do something
end if
function getVal( v, replacementVal )
if v is nothing then
getVal = replacementVal
else
getVal = v
end if
end function
这篇关于VBScript的条件短路的解决方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!