|
| declaring |
Dim <variable name> As <data type>
| 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 |
| Examples |
Dim intA As Integer
Dim dblTaxRate As Double
Dim MyName As String
| Constants |
Make program easier to read 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.
| Text box |

|
Objects
(Design the Form) |
form
label (2)
text box txt
buttons (2)
|
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 |
|
Algorithm (Code)
|
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.

When using integers, the operator \ (backslash) returns the integer portion of the result of division. Therefore:
|
7 \ 2 return 3 |
|
|
12 \ 5 returns 2 |
|
|
35 \ 7 returns 5 |
|
|
35 \ 6 returns 5 |
The Mod operator returns the remainder of division. Thus:
|
7 Mod 2 returns 1 |
|
|
12 Mod 5 returns 2 |
|
|
35 Mod 7 returns 0 |
|
|
35 Mod
6 returns 5 |
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.
|
a form (frmHello) |
|
|
a frame to contain the option buttons (frmLanguages) |
|
|
3 option button in the frame (optEnglish, optFrench, optFrench) |
|
|
a label to display the answer (lblAnswer) |
|
|
a command button the end the program (cmdEnd) |
To develop the program, we will follow the usual three steps:
|
Design the Form |
|
|
Set the Properties |
|
|
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).