VBScript conditional short-circuiting workaround

后端 未结 9 2137
心在旅途
心在旅途 2021-02-19 05:15

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

相关标签:
9条回答
  • 2021-02-19 05:40

    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
    
    0 讨论(0)
  • 2021-02-19 05:42

    You may be able to just use Else to catch nulls, ""s, etc.

    If UCase(Rs("myField")) = "THING" then
      'Do Things
    elseif UCase(Rs("myField")) = "STUFF" then
      'Do Other Stuff
    else
      'Invalid data, such as a NULL, "", etc.
      'Throw an error, do nothing, or default action
    End If
    

    I've tested this in my code and it's currently working. Might not be right for everyone's situation though.

    0 讨论(0)
  • 2021-02-19 05:47

    Or perhaps I got the wrong end of the question. Did you mean something like iIf() in VB? This works for me:

    myField = returnIf(isNothing(rs("myField")), 0, rs("myField"))
    

    where returnIf() is a function like so:

    function returnIf(uExpression, uTrue, uFalse)
        if (uExpression = true) then returnIf = uTrue else returnIf = uFalse : end if
    end function
    
    0 讨论(0)
提交回复
热议问题