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?

Kevin
  • 5,874
  • 3
  • 28
  • 35

9 Answers9

10

Nested IFs (only slightly less verbose):

if not isNull(Rs("myField")) Then
   if Rs("myField") <> 0 then
busse
  • 1,761
  • 14
  • 25
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
oglester
  • 6,605
  • 8
  • 43
  • 63
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.

dewde
  • 603
  • 1
  • 7
  • 12
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"))
Bond
  • 16,071
  • 6
  • 30
  • 53
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
Cirieno
  • 481
  • 2
  • 8
  • 1
    But isn't `rs("myField")` calculated while `returnIf` is called? In that case it's not short-circuit evaluation... – realsonic Jul 20 '18 at 05:27
  • No, this function doesn't parse the inputs until you need them, so they don't get evaluated (although you could pass an expression in as the second or third param, in which case it would get evaluated on the fly). And this is quite the zombie thread -- this reply is one month shy of 10 years old! – Cirieno Aug 23 '18 at 18:24
  • 1
    Dispite of this (zombie thread) I was seaking an answer... And yes, it's was needed for real work... – realsonic Aug 24 '18 at 19:17
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
Marshall
  • 1,095
  • 1
  • 12
  • 17
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
Cirieno
  • 481
  • 2
  • 8
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.

JemWritesCode
  • 502
  • 6
  • 17
0

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

Danimal
  • 7,672
  • 8
  • 47
  • 57
  • 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 Sep 12 '08 at 18:43