Link to home
Start Free TrialLog in
Avatar of ljaques
ljaques

asked on

'IF' with 'AND'/'OR' ...

This is VERY ODD...

pos = instr(1,"HELLO WORLD","XX")

'pos will = 0 since "XX" wont be found in it
if pos > 0 or (pos > 0 and mid("HELLO WORLD",pos)="WORLD") then
   'process whatever
endif

The if statement above is ILLEGAL in VB because eventhough i have a "pos > 0" for the second condition in the "if" statement it still processes the entire condition making the "mid" function invalid since pos cannot be < 1:
ILLEGAL: mid("HELLO WORLD",0)="WORLD")

This is correct but since i had the "pos > 0" at the beginning of the 2nd condtion it should not have allowed the rest of the condition to be processed thus preventing the mid() function from returning an error. I have NEVER seen any other programming language do this before. It is like vb is checking the validity of the ENTIRE line before it begins to process it.  Really ODD!  Is there a way around this, preferably keeping my entire conditions all in one "if" statement?



ASKER CERTIFIED SOLUTION
Avatar of mark2150
mark2150

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of vikiing
vikiing

Let me say, friend Mark, the problem is not due to the fact that "it's a compiler", because some other compilers have an option named "boolean short-circuit" (like Pascal, or antique Turbo Basic) in which, when there's no doubt about the final result of a logical expression, program stops evaluating the rest of it,  and branches accordingly. In Ljaques' example, when Pos > 0, it would suffice to execute the "then" portion of the IF, without seeing what happens with the rest, so skipping the potential error of MID$()

Friend Ljaques: unfortunately, this is one of the ton of stupid things Microstuff did when developed VB. The most atonishing thing I've found is the unability to use the "#" char in Format$(), as it worked since decades ago...   ~~~>:(
ljaques...
crummy hack but will keep it on one line
If pos > 0 Or (Mid("HELLO WORLD", IIf(pos, pos, 1)) = "WORLD") Then
   'process whatever
End If