Does VBA have an "OrElse" operator?

Gemma,

You've hit the point right on the head.

I bet that if we knew more about Jal's needs, we could indeed do as you suggested, rewrite the code so there's no need for any weird or exotic construction.

Also, the controvesy about what machine code are being generated by what coding has pretty much faded into background. Back then when each cycle of computer were sacred and holy, and shouldn't be wasted at all, it was common for programmers to argue over finer points, but with computing powers having gained so much and becoming insanely cheap, there is really not much incentive to gain 7% extra speed by writing the whole program in assembly language, which provides the programmers much more control over how things are done than a higher level language.

I think for that reason, the needs to write efficient code at expense of readability has quite reduced, and thus we see more emphasis on writing clean code and admittedly a bit more of dogmatic positions towards arbitrary execution such as goto statement.

And there's a raptor, too!
goto.png
 
Back then when each cycle of computer were sacred and holy, and shouldn't be wasted at all, it was common for programmers to argue over finer points,

After we moved off card based machines I have never worked on small computers as we made a descision that our first IBM 360 should have a decent amount of memory so we got 512 , k that is.;)

Brian

PS loved your cartoon.
 
Here's my stab, it's in vb.net so not sure if correct syntax in VBA.

Build a wrapper function:

Code:
Public Function myOrElse(func1 as Boolean, func2 as Boolean) as Boolean

If func1 Then Return func1
Return func2

End Function

and call it in your code

Code:
If myOrElse(func1,func2) Then
...
End If

EDIT: GOTO is the work of the devil :D
 
Last edited:
Dan-cat,

You win teh internets! I'm impressed!

Just a bit fixing for VBA as it doesn't have "Return" statement:

Code:
Public Function myOrElse(func1 as Boolean, func2 as Boolean) as Boolean

If func1 Then 
    myOrElse=True
    Exit Function
Elseif func2 Then
   myOrElse=True
   Exit Function
Else
   myOrElse=False
End If

End Function

At least we are not using GoTo...
 
I think for that reason, the needs to write efficient code at expense of readability has quite reduced, and thus we see more emphasis on writing clean code and admittedly a bit more of dogmatic positions towards arbitrary execution such as goto statement.

True but this approach can trap the unwary.

Take the IIf command for example. As said before both true and false statements are evaluated which can lead to unplanned results for the sake of brevity.

For instance

Code:
IIf(IsNothing(myObject),,myObject.SaleNo = 1)

will throw an error if the statement is true.

The IIf statement should be kicked into touch IMO :)
 
Yeah, this is actually similar to traps that Perl programmers fall in; they like to show off how much they can do in one line but years later (actually, no, make it a month later) they're all "WTF does that does?!?"
 
Dan-cat,

You win teh internets! I'm impressed!

Just a bit fixing for VBA as it doesn't have "Return" statement:


Awww no Return statement, boooooo!

Can I trim this down...


Code:
Public Function myOrElse(func1 as Boolean, func2 as Boolean) as Boolean

If func1 Then 
    myOrElse=True
    Exit Function
Elseif func2 Then
   myOrElse=True
   Exit Function
Else
   myOrElse=False
End If

End Function


to


Code:
Public Function myOrElse(func1 as Boolean, func2 as Boolean) as Boolean

If func1 Then 
    myOrElse=True
    Exit Function
End If

myOrElse = func2

End Function

reason being I hate not having a fail-safe return on a function after all the If statements have been evaluated. I know your Else statement takes care of it but it's just a little quirk of mine.

I have been away from VBA so long I'd forgotten no return statements. Wow, I'm getting old. :p
 
Much better and more efficient. I'm impressed. :) You're definitely right- it's better to let the function evaluate the second condition and return the results than to hard code a False in a Else clause.

Yes, it's weird that VB/VBA doesn't support Return as it does two things at same time; exit the function with a variable, whereas it's a two step affair in VB/VBA. I suspect it was a decision to make the code more readable or easier to understand by doing only one thing at a time.
 

Users who are viewing this thread

Back
Top Bottom