asp-classic – VBScript条件短路解决方法

前端之家收集整理的这篇文章主要介绍了asp-classic – VBScript条件短路解决方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个大的经典的ASP应用程序,我必须维护,我反复发现自己被缺乏短路评估能力所阻挠.例如,VBScript不会让你摆脱:
  1. if not isNull(Rs("myField")) and Rs("myField") <> 0 then
  2. ...

…因为如果Rs(“myField”)为空,则在第二个条件中会收到一个错误,将null与0进行比较.因此,我通常会这样做:@H_301_5@

  1. dim myField
  2. if isNull(Rs("myField")) then
  3. myField = 0
  4. else
  5. myField = Rs("myField")
  6. end if
  7.  
  8. if myField <> 0 then
  9. ...

显而易见,这是一个令人震惊的消息.围绕这个大代码库,我发现最好的解决方法是使用原始程序员写的函数,称为TernaryOp,它基本上以三进制类似于操作符的功能移植,但是我仍然使用一个临时变量需要一个更全面的语言.有没有更好的办法? VBScript中确实存在一些超级秘密的短路方式?@H_301_5@

解决方法

也许不是最好的方法,但它肯定是有效的…另外,如果你在vb6或.net,你可以有不同的方法投射到正确的类型.
  1. if cint( getVal( rs("blah"),"" ) )<> 0 then
  2. 'do something
  3. end if
  4.  
  5.  
  6. function getVal( v,replacementVal )
  7. if v is nothing then
  8. getVal = replacementVal
  9. else
  10. getVal = v
  11. end if
  12. end function

猜你在找的asp.Net相关文章