Tuesday, February 8, 2011

Visual Basic 6.0 - Session 5


•  Add a new menu heading Selections with two subheadings, Binary Selection and Multiway Selection.  

•  Write a program to convert inches to centimetres OR centimetres to inches (using the conversion 1 inch = 2.54 centimetres).  
•  Use option buttons (from the toolbox) for the user to indicate whether the conversion is inches to centimetres or centimetres to inches.
•  Use IF statements to determine which formula to use based on which option button is selected.  Option buttons are mutually exclusive, i.e. only one can be selected at a time.  
•  Connect to menu heading Binary Selection.  
•  Run the application to ensure that it is working correctly.  
•  Use your calculator to verify the results.  Try it out with some test data including very large numbers, very small numbers, zero, negative numbers, 0.000000987654.  

Multiway selection
In Activity 5 we looked at an example of binary selection.  If the selection involves more than two alternatives, you can use nested If statements but this becomes complicated and leads to hard-to-read code.  It is better to use Case statements.  Here is the syntax for multiple selection through Case statements.
  
Select Case Expression
 Case value
   [One or more VB statements]
 Case value
   [One or more VB statements]
 Case value
   [One or more VB statements]
 Case Else
   [One or more VB statements]
End Select

For example:
 
Select Case intAge  
Case Is < 6
   lblTitle.Caption = “Preschool”
 Case 6 To 11
   lblTitle.Caption = “Primary School”
 Case 12 To 18
   lblTitle.Caption = “Secondary School”
 Case Else
   lblTitle.Caption = “Adult”
End Select

No comments:

Post a Comment