| 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-3: If Statements
Exercise 4-2: Number Guessing Game (continued ...)
But first lets learn how to choose a random value! When the program first starts, and again when the Play Again button is pressed, a random integer between 1 and 100 must be chosen. Most programming languages have a means of generating random numbers. In Visual Basic there is a class, the Random class, that can be used to generate random numbers.
Examine this class in the Object Browser. It can be found in the System namespace, and is shown in Figure 4-4. Note that it has methods such as Next and NextDouble which seem to be what we will need in order to generate random numbers. Read the information provided about these methods.
To use the class we first have to create an object of the class, referring to this object by a name that we choose and declare as a variable. The variable type will not be Integer or Double, etc., such as we have used before but rather the type will be Random. I.e. classes can define the datatype of variables. Such variables are called object variables because they name objects rather than "primitive" data types such as Integer or Double.