Understanding Decision Structures


Decision structures control whether other statements execute , according to the results of conditional statements . In the function strNewTask, for instance, there was an If Then Else decision structure that determined what to set for the function's return value, based on the results of a test.

There are four different decision structures, each suitable for different situations, but they are all very similar:

If Then.       The simplest of all decision structures, the If Then statement takes just one action if just one condition is met. If the condition is not met, the statement does nothing:

 If ActiveProject.Tasks(1).PercentComplete = 100 Then MsgBox "Task complete!" 

If the first task's PercentComplete property is not 100, the line is skipped .

Note  

Unlike the other decision structures, If Then can either be a single-line statement or a block . An If Then statement is one test, one action, and one line of code. An If Then block is still one test, but it can perform many actions over several lines of code.

Note  

Because a decision block is essentially a miniature procedure, it must have some sort of ending statement. The End If statement at the end of a decision block works just like the End Sub or End Function at the end of a procedure.

If Then Else.       A more common form of decision structure is the one used in the function strNewTask, If Then Else. Unlike the If Then statement, this structure performs a test to determine not whether it should take action, but which action it should take. Something will always happen because meeting the condition results in one action, and anything else results in the second:

 If ActiveProject.Tasks(1).PercentComplete = 100 Then     MsgBox "The task is complete!" Else     MsgBox "The task is not finished." End If 

In this situation, if the task's PercentComplete property is 100, the dialog box appears just as it does for the If Then statement. The difference here is that anything less than 100 also displays a dialog box.

If Then ElseIf.       A variation on the If Then Else type is the If Then ElseIf block. In this variation, there can be any number of ElseIf statements, each of which performs a further refinement of the test defined in the original If Then statement. For example:

 If ActiveProject.Tasks(1).PercentComplete = 100 Then     MsgBox "The task is complete!" ElseIf ActiveProject.Tasks(1).PercentComplete >= 50 Then     MsgBox "The task is more than half finished." ElseIf ActiveProject.Tasks(1).PercentComplete > 0 Then     MsgBox "The task has started, but is less than 50% complete." Else     MsgBox "The task has not started!" End If 

Select Case.      This is another way to test for a large number of conditions, but in a more readable way than an If Then ElseIf block.

 Select Case ActiveProject.Tasks(1).PercentComplete     Case Is = 100         MsgBox "The task is complete!"     Case Is >= 50         MsgBox "The task is more than half finished."     Case Is > 0         MsgBox "The task has started, but is less than 50% complete."     Case Is = 0         MsgBox "The task has not started!" End Select 
Note  

The Is keyword used here is only required because these Case statements are also comparisons, which means they have to be compared with something. Typically, the Select Case statement contains a reference to a variable, and each Case statement represents a different possible value.

As you can see, using the Select Case structure makes it much easier to find the different conditions for displaying a dialog box. In fact, the statement

 Case Is = 0 

is even more explicit than is strictly necessary because we've already eliminated every other possible value for the PercentComplete property. In this instance, a simple

 Case Else 

would have been sufficient, and is probably just as readable.

Tip  

The more explicit, the better       The value in using an explicit construction instead of the Case Else statement can be shown by simply asking this question: What if you didn't know that the range of possible values for the PercentComplete property was 0 “100?




Microsoft Office Project 2003 Inside Out
Microsoft Office Project 2003 Inside Out
ISBN: 0735619581
EAN: 2147483647
Year: 2003
Pages: 268

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net