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 get away with:

if not isNull(Rs("myField")) and Rs("myField") <> 0 then
...

...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
...

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?

4

9 回答 9

10

Nested IFs (only slightly less verbose):

if not isNull(Rs("myField")) Then
   if Rs("myField") <> 0 then
于 2008-09-12T17:45:12.770 回答
7

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
于 2008-09-12T17:50:44.553 回答
5

I always used Select Case statements to short circuit logic in VB. Something like..

Select Case True

Case isNull(Rs("myField"))

    myField = 0

Case (Rs("myField") <> 0)

    myField = Rs("myField")

Case Else

    myField = -1        

End Select

My syntax may be off, been a while. If the first case pops, everything else is ignored.

于 2008-09-12T18:26:33.780 回答
4

If you write it as two inline IF statements, you can achieve short-circuiting:

if not isNull(Rs("myField")) then if Rs("myField") <> 0 then ...

But your then action must appear on the same line as well. If you need multiple statements after then, you can separate them with : or move your code to a subroutine that you can call. For example:

if not isNull(Rs("myField")) then if Rs("myField") <> 0 then x = 1 : y = 2

Or

if not isNull(Rs("myField")) then if Rs("myField") <> 0 then DoSomething(Rs("myField"))
于 2014-03-22T01:37:15.290 回答
3

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
于 2008-09-26T13:42:35.917 回答
1

Yeah it's not the best solution but what we use is something like this

function ReplaceNull(s)
    if IsNull(s) or s = "" then
        ReplaceNull = "&nbsp;"
    else
        ReplaceNull = s
    end if
end function
于 2008-09-12T17:58:59.867 回答
0

Would that there were, my friend -- TernaryOp is your only hope.

于 2008-09-12T17:49:35.677 回答
0

Two options come to mind:

1) use len() or lenb() to discover if there is any data in the variable:

if not lenb(rs("myField"))=0 then...

2) use a function that returns a boolean:

if not isNothing(rs("myField")) then...

where isNothing() is a function like so:

function isNothing(vInput)
    isNothing = false : vInput = trim(vInput)
    if vartype(vInput)=0 or isEmpty(vInput) or isNull(vInput) or lenb(vInput)=0 then isNothing = true : end if 
end function
于 2008-09-26T13:36:34.853 回答
0

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.

于 2016-07-13T14:11:07.010 回答