| Chapter 3- Data Types and Operators | Boolean Expressions Page 2 3 4 |
| The If Statement Page 2 3 4 5 6 7 8 9 | |
| Arithmetic Operations Page 2 3 4 5 6 | Boolean Operators and Nested If Statements Page 2 3 4 5 6 7 |
| Events and Sequential Processing Page 2 3 4 5 | More Examples Page 2 3 4 5 6 7 8 9 10 11 12 |
| Datatypes and Conversions Page 2 3 4 5 6 7 | Using Check Box and Option Controls Page 2 3 4 5 6 7 8 9 10 |
| Variable Declarations - Local and Global Page 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | Exercises Page 2 3 4 5 6 7 8 |
| Chapter 4- Selection Statements | Review Questions |
| Introduction | |
4-5: Boolean Operators
Exercise 4-3: The Nested If Structure
Putting all of this together we have
declare variable as Integer type, and a variable as Date
type
if (valid date value in textbox) then
convert
string to date
extract the year, assigning it to the Integer
variable
assign this integer value to Text property of lblYear
if
(divisible by 4 but not by 100 OR divisible by 400) then
is leap year (assign message to Text property of
lblMessage)
else
is not a leap year (assign
message ... )
end if
else
assign
input error message to Text property of lblMessage
end if
Notice that the If statement that determines whether or not the year is a leap year (and assigns the appropriate message to the Text property of the message label object) is entirely contained within the first branch of the If statement that determines whether the user input is a valid date.
Such nested If statements are very common, and you should aim to develop an easy familiarity with them.
Use the Object Browser to read about the ToDateTime conversion functions. These can be found in the System.Convert class. Why are there so many ToDateTime functions in this class? What is the diference detween them?
And, finally, to extract the year from a Date value look for the Year function, found in the Microsoft.VisualBasic DateAndTime module.
The code for the Check It button click event can now be completed.
Complete this project and test it thoroughly. Can you input data in the text box that crashes the program? Is there any aspect of the interface that should be improved?