views:

944

answers:

8

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?

+2  A: 

Nested IFs (only slightly less verbose):

if not isNull(Rs("myField")) Then
   if Rs("myField") <> 0 then
busse
+1  A: 

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

Danimal
Classic VB doesn't have a real ternary op either, just the IIf() function (immediate if). But even that is still a function, so _all_ function arguments must be evaluated before passing to the function.
Joel Coehoorn
+1  A: 

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
Greg Ogle
A: 

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
Marshall
+1  A: 

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.

dewde
A: 

@Oglester, it looks like there's a typo in your function call, but that's my preferred answer in principle. It's fairly terse, and makes the semantics similar to using the SQL ISNULL and COALESCE functions, which is familiar ground for me.

I guess there is an example or two of the original guy doing something like that in my app's codebase, but only for more specific purposes. I see now where I could use those methods in a more generalized way.

Thanks to everybody who's answered, and I'll still be checking back to see if Ultra VBScript Ninja with the super-secret Answer I'd Really Like to Hear eventually proves us all wrong with our naysaying about VBScript's capabilities.

kcrumley
I believe I fixed it. Thanks.
Greg Ogle
A: 

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
Cirieno
+2  A: 

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
Cirieno