Kev*_*ley 14 vbscript asp-classic
我有一个很大的经典ASP应用程序,我必须维护,我反复发现自己因缺乏短路评估能力而受阻.例如,VBScript不会让你逃脱:
if not isNull(Rs("myField")) and Rs("myField") <> 0 then
...
Run Code Online (Sandbox Code Playgroud)
...因为如果Rs("myField")为null,则在第二个条件中出现错误,将null与0进行比较.所以我通常最终会这样做:
dim myField
if isNull(Rs("myField")) then
myField = 0
else
myField = Rs("myField")
end if
if myField <> 0 then
...
Run Code Online (Sandbox Code Playgroud)
显然,冗长是非常可怕的.看看这个庞大的代码库,我发现最好的解决方法是使用原始程序员编写的一个函数,名为TernaryOp,它基本上采用三元运算符式功能,但我仍然坚持使用一个不会的临时变量在功能更全面的语言中是必要的.有没有更好的办法?VBScript中确实存在一些超级秘密的短路方法吗?
嵌套的IF(仅略微冗长):
if not isNull(Rs("myField")) Then
if Rs("myField") <> 0 then
Run Code Online (Sandbox Code Playgroud)
也许不是最好的方法,但它确实有效...而且,如果你在vb6或.net中,你可以使用不同的方法转换为正确的类型.
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
Run Code Online (Sandbox Code Playgroud)
我一直使用Select Case语句来简化VB中的逻辑.就像是..
Select Case True
Case isNull(Rs("myField"))
myField = 0
Case (Rs("myField") <> 0)
myField = Rs("myField")
Case Else
myField = -1
End Select
Run Code Online (Sandbox Code Playgroud)
我的语法可能已关闭,已有一段时间了.如果弹出第一个案例,则忽略其他所有案例.
| 归档时间: |
|
| 查看次数: |
15215 次 |
| 最近记录: |