Lesson 2
(Chapter 4)


Variables

bullet declaring

Dim <variable name> As <data type>

bullet data types
 
Data Type Storage Range Prefix   Examples
Single
4 bytes
-3.37E+38
3.37E+38
sgl
sglTestScore
Double
8 bytes
-1.67D+308
1.67D+308
dbl
dblRadius
Integer
2 bytes
-32768
32767
int
intAge
Long
4 bytes
-2147483648
2147483648
lng
lngMilesToAlphaCenturi
Currency
8 bytes
-922E+14
922E+14
cur
curAmountTendered
String 255 bytes ASCII codes str strFirstName
Boolean 1 byte 0 or 1 bln blnYesOrNo
bullet Examples

Dim intA As Integer
Dim dblTaxRate As Double
Dim MyName As String

bullet Constants
bullet Make program easier to read
bullet Make program easier to modify

Const dblPi As Double = 3.14
Const dblTaxRate As Double = 6.0


Keeping yourself Honest!

Historically, the BASIC language was weakly typed and did not force the programmer to declare variables before using them. Visual Basic stays true to this rather unfortunate tradition. Not declaring variables is a major cause of bugs (or "side affects") in a program. Visual Basic DOES allow the programmer to set the VB environment to force variable declarations. I REQUIRE you to do this, both in the lab AND at home. Any programs that do not have the VB command Option Explicit will be returned! This command is placed in the General sections of the program. You can also set up VB to do this automatically. Again, I require you to do this. In the VB Tools menu, select Options.

Under the Editor tab, check the Require Variable Declaration check box.


Obtaining A Value from the User

bullet Text box

bullet

Objects (Design the Form)

bullet

form

bullet

label (2)

bullet

text box txt

bullet

buttons (2)

bullet

Properties

  Name Caption
form frmCircleArea Circle Area Program

label

lblTextLabel
lblAnswer
Enter the radius of the circle
"" (nothing!!)

text box

txtRadius
No caption property
Text = ""

buttons (2)

cmdCalculate
cmdExit
&Calculate
E&xit
bullet

Algorithm (Code)

bullet

set constant for Pi (dblPi)

bullet

dimension double variables for Radius (dblRadius) and CircleArea (dblCircleArea)

bullet

assign value in text box to dblRadius variable

bullet

calculate area (dblPi x dblRadius x dblRadius)

bullet

place answer in lblAnswer


Class Assignment: Write a program that uses the interface below to ask the user to  type in their name.

 

It then displays the message shown.


Integer Arithmetic (\ and Mod)

When using integers, the operator \ (backslash) returns the integer portion of the result of division. Therefore:

bullet

7 \ 2 return 3

bullet

12 \ 5 returns 2

bullet

35 \ 7 returns 5

bullet

35 \ 6 returns 5

The Mod operator returns the remainder of division. Thus:

bullet

7 Mod 2 returns 1

bullet

12 Mod 5 returns 2

bullet

35 Mod 7 returns 0

bullet

35 Mod 6 returns 5


Option (Radio) Buttons

Another method of getting input from the use is using option (radio) buttons. Option buttons are a good choice when you want to force the user to select one and only one from a list of options. Option buttons work as a group. Selecting one automatically deselects the other choices. Therefore, it is good form to place option buttons in a "container." This is particularly important if there is more than one set of option buttons on a form. To illustrate the use of option buttons, let's create a version of the "Hello, World" program that asks the user to select the language to display the "Hello, World" message. This is a good application for an option button because we want to force the user to select one of the languages. The output will be displayed in a label.

bullet

a form (frmHello)

bullet

a frame to contain the option buttons (frmLanguages)

bullet

3 option button in the frame (optEnglish, optFrench, optFrench)

bullet

a label to display the answer (lblAnswer)

bullet

a command button the end the program (cmdEnd)

To develop the program, we will follow the usual three steps:

bullet

Design the Form

bullet

Set the Properties

bullet

Write the Code


Step 1: Design the Form

The option button and frame tools are found in the toolbox.

The form (before setting the properties) will look like this:

Step 2: Set the Properties

Set the following properties:

Object Name Caption
Form frmHello Hello, Hola Bonjour
Label lblAnswer "" <-- Nothing
Frame frmLanguage Select your language
Option Button optEnglish English
Option Button optSpanish Spanish
Option Button optFrench French
Command Button cmdEnd End

Now your form should look like this:

Step 3: Write the Code

optEnglish lblAnswer.Caption = "Hello, World
optSpanish lblAnswer.Caption = "Hola, mundo"
optFrench lblAnswer.Caption = "Bonjour, le monde"

Homework

Case Study (page 4-19)

Complete exercises 6, 7, 11, and 13 in chapter 4 (beginning on page 4-28).