Solved

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

Posted on 1998-12-16
3
119 Views
Last Modified: 2010-05-03
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
Comment
Question by:ljaques
3 Comments
 
LVL 12

Accepted Solution

by:
mark2150 earned 20 total points
ID: 1450591
It's a compiler. Yes, it's checking the validity of the entire line.

Break it out into two statements.

M
0
 
LVL 3

Expert Comment

by:vikiing
ID: 1450592
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
 
LVL 1

Expert Comment

by:muffinthedog
ID: 1450593
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

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question