VFP Tutorial - Do Case

The Do Case structure allows a choice between several actions based on the result of a logical expression:

Do Case
   Case _TALLY = 0
     Wait Window 'No records found'
   Case _TALLY = 1
     Wait Window 'One record found'
   Otherwise
     Wait Window 'Many records found'
EndCase

Each Case statement is followed by an expression which must evaluate to true or false. FoxPro evaluates each of these in sequence.

If the expression following the first Case statement evaluates as true (.T. in FoxPro) then the statements between this Case and the next one will be executed. The rest of the structure will be skipped and execution will continue on the line following the EndCase statement.

If the first expression evaluates as false (.F. in FoxPro) then the next Case statements will be evaluated in turn until one is found to be true. If none are found to be true then the code following Otherwise will be executed.

Notes

_TALLY is a variable maintained by FoxPro which holds the number of records processed by the last command. It is very useful in situations like this where you need to know how many records have been found.

Each logical condition must evaluate as either .T. or .F. because the logical values in FoxPro are a separate data type. Unlike in many other languages, an integer result will not be interpreted as a logical value.

Be careful to get the tests in the right order when designing a Case structure. Remember that the expressions are evaluated in sequence and put the toughest test first. If for example you need to test whether something is greater than 1 or greater than 10 then you must test for greater than 10 first. If you test for greater than 1 first then a value such as 11 would pass that test and the greater than 10 test would never be reached. This sort of mistake can be difficult to detect in tests because the program runs without error and will give the right result most of the time.

The Otherwise clause is optional. If it is not included and all the individual Case statements evaluate as false then none of the code will be executed.

It is good practice to include an Otherwise clause even if there is no action which needs to be taken. If you are positive that the individual Case statements cover every possible situation then add an Otherwise and display an error message.

Each Case can have an expression based on a different variable. This example works out the method of delivery from the value of the order, the weight of the goods and the country to which it has to be delivered.

Do Case
   Case lyCost > 1000
     *-- Free delivery for large orders
     lcDelMethod = 'Free'
   Case lnWeight > 10
     *-- Buyers must collect cheap, heavy orders
     lcDelMethod = 'Collect'
   Case lcCountry <> 'UK'
     *-- Airmail for a light, cheap, overseas delivery
     lcDelMethod = 'Airmail'
   Otherwise
     *-- Must be light, cheap, inland delivery
     lcDelMethod = '2nd class mail'
EndCase

Back to FoxPro program control structures.


Introduction | Environment | Project | Tables | Forms | Navigation | Executable

Access Tips

FoxPro Tips

General Tips

 

Related Items

Visual FoxPro Tutorial - Program control

Program control structures in Visual FOxPro

Read More

Visual FoxPro Tutorial - Program Code

Visual FoxPro Tutorial - Writing program code

Read More

Visual FoxPro Tutorial - Development Environment

The Integrated Development Environment (IDE) for Visual FoxPro

Read More

Visual FoxPro Tutorial - Build an executable

Visual FoxPro Tutorial - Build a Windows executable from a Foxpro project

Read More

Visual FoxPro Tutorial - Form Designer

Visual FoxPro Tutorial - Using the Form Designer

Read More