Visual Basic Variables and Operators

Using variables to store data in your applications

 

Program Statements

A program statement is any combination of Visual Basic keywords, properties, functions, operators, and symbols that collectively create a valid instruction recognized by the VB compiler.

It does not necessarily need to be complicated, it can be as simple as Beep.

 

Program Statements cont...

The program statement is normally a combination of elements such as

Label1.Caption = Time

The order and method that you use to construct a programming statement is called the language’s syntax.

Visual Basic helps us quite a lot with the syntax, so that we can concentrate on developing programs quickly.

 

Variables to Store Information

A variable is a temporary storage location for data in your program.

You can use as many variables as you wish in your programs and they can contain words, numbers, dates or properties.

Variables are very useful because they allow you to assign a short and easy-to-remember name to a piece of data you plan to work with.

The Dim Statement

To make a reservation for a variable in our program we need to explicitly declare it.

To explicitly declare a variable we type the variable name after the Dim statement.

Dim stands for dimension.

This action reserves room in memory for the variable when the program runs, and lets VB know what type of data it should expect to see later.

Dim Statement cont...

For example the following statement will reserve a place for a variable named LastName in a program:

Dim LastName

After you declare the variable you are then free to assign information to it in your code.

For example the following will assign the last name "Winegar" to the LastName variable:

LastName="Winegar"

 

 

Dim Statement cont...

After this assignment, the LastName variable can be used in place of the name "Winegar" in your code.

For example, the assignment statement

Label1.Caption = LastName would display Winegar in the first label (Label1) box on your form.

 

Declaring Variable without Dim

It is possible to declare a variable without the Dim statement.

This is what is called Implicit Declaration.

To use it all you need to do is to skip the Dim statement.

For example:

LastName="Jones"

 

Declaring Variable without Dim cont...

Using Impicit Declaration has a speed advantage when coding because you eliminate the step where you declare your variable.

However, it has the disadvantage of not being able to take advantage of VBs error message handling.

VB can automatically notify you of a mis-spelled variable.

 

Error Notification for Variables

To turn on the error notification for the Dim statement go to Tools>>Options and the Editor tab and place a check next to the "Require Variable Declaration."

Warning--this will mean that you will always need to declare your variables with a Dim statement.

 

Using Variables in a Program

Using Variables

Variables can maintain the same value throughout a program or they can change values depending on your needs.

Let’s create a program the changes the value of a variable.

 

Change the value of a variable program example

Create the skeleton program that contains two label boxes and two command buttons.

Write the code for declaring 2 last names and writing one to the Label1 box and the other to the Label2 box.

Dim LastName

LastName = "Winegar"

Label1.Caption = LastName

LastName = "Lehto"

Label2.Caption = LastName

 

Using a Variable to Store Input

One good use of a variable is to hold information input from the user.

One way to do this is to use the InputBox function to display a dialog box on the screen and then to store the text the user types in a variable.

Get input using the InputBox

Create the Inputbox skeleton program

Write the following code to declare two variables (Prompt and FullName) and assign a group of characters (or a text string) to the Prompt variable.

Dim Prompt, FullName

Prompt = "Please enter you name."

 

Get input using the InputBox cont...

The set of code calls the InputBox function and assigns the result of the call (the text the user inputs) to the FullName variable.

InputBox is a special VB function that displays a dialog box on the screen and prompts the user for input.

The fourth statement in the procedure places the user’s name in the Caption property of the Label1 object, which it displays on the form.

What is a Function

We have used a couple of different functions so far in our programs.

A function is a special VB keyword that performs meaningful work (like Date, Time, InputBox).

VB functions often use more than one argument to define there activities. These arguments are separated by commas.

For example the following statement has 2 arguments

FullName=InputBox$(Prompt, Title)

 

Message Box

We can display a message box to the user confirming their input by using the VB MsgBox function.

The syntax for a MsgBox is

Name_you _give=MsgBox(Message, NumberOfButtons, Title)

Where Message is the text to be displayed, NumberOfButtons is the button style you will use, and title is the text displayed on the box title bar.

Message Box cont...

Modify Command1 to look like the following:

Dim Prompt, FullName, Response, Default

10 Prompt = "Please enter you name."

FullName = InputBox$(Prompt)

Response = MsgBox(FullName, vbYesNo, "Is this correct?")

If Response = vbYes Then

Label1.Caption = FullName

Else

GoTo 10

End If

End Sub

 

The Mortgage Calculator

Let’s apply some of what we learned to build an application that calculates the monthly payment for the loan on a house.

We will need to use Dim to define the inputs and the formula
Principle*MonthInt/(1-(1+MonthInt))^(Years*12)

We will also use the build in functions for several normal mathematical operators.

Note: It is helpful to divide the formula into Numerator (on top) and Denominator (on bottom).

 

The Command1_Click Code