• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 124
  • Last Modified:

'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?



0
ljaques
Asked:
ljaques
1 Solution
 
mark2150Commented:
It's a compiler. Yes, it's checking the validity of the entire line.

Break it out into two statements.

M
0
 
vikiingCommented:
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...   ~~~>:(
0
 
muffinthedogCommented:
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

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now