Variables

Objectives

After studying this unit, you will be able to:

Explain the operators available

Understand the concept of variables

Introduction

In order to store values temporarily we make use of variables. Variables have a name that helps to refer to it and a data type that determines the kind of data the variable can store. Constants are used when we need the values to remain constant throughout the execution of an application.

Using constants can make your code more readable by providing meaningful names instead of numbers. Data types control the internal storage of data in Visual Basic. By default, Visual Basic uses the Variant data type. We will now discuss each of them in detail.

2.1 Variables

Variables are used to temporarily store values during the execution of an application. Variables have a name and a type. You can think of a variable as a placeholder in memory for an unknown value.

Example: Imagine you are creating a program for a fruit stand to track the sales of

apples. You don’t know the price of an apple or the quantity sold until the sale actually occurs.

You can use two variables to hold the unknown values — let’s name them ApplePrice and ApplesSold. Each time the program is run, the user supplies the values for the two variables. To calculate the total sales and display it in a Textbox named txtSales, your code would look likethis:

txtSales.txt = ApplePrice * ApplesSold

The expression returns a different total each time, depending on what values the user provides.

The variables allow you to make a calculation without having to know in advance what the actual inputs are.

In this example, the data type of ApplePrice is Currency; the data type of ApplesSold is an integer. Variables can represent many other values as well: text values, dates, various numeric types, even objects.

You use assignment statements to perform calculations and assign the result to a variable:

ApplesSold = 10 // The value 10 is passed to the variable.

ApplesSold = ApplesSold + 1 // The variable is incremented.

You declare a variable with the Dim statement, supplying a name for the variable:

Dim variablename [As type]

Variables declared with the Dim statement within a procedure exist only as long as the procedure

is executing. When the procedure finishes, the value of the variable disappears. In addition, the

value of a variable in a procedure is local to that procedure — that is, you can’t access a variable

in one procedure from another procedure. These characteristics allow you to use the same

variable names in different procedures without worrying about conflicts or accidental changes.

A variable name:

 Must begin with a letter.

Can’t contain an embedded period or embedded type-declaration character.

Must not exceed 255 characters.

Must be unique within the same scope, which is the range from which the variable can bereferenced — a procedure, a form, and so on.

The optional As type clause in the Dim statement allows you to define the data type or object type of the variable you are declaring. Data types define the type of information the variable stores. Some examples of data types include String, Integer, and Currency. Variables can also contain objects from Visual Basic or other applications. Examples of Visual Basic object types, or classes, include Object, Form1, and TextBox.

There are other ways to declare variables:

 Declaring a variable in the Declarations section of a form, standard, or class module, rather than within a procedure, makes the variable available to all the procedures in the emodule.

Declaring a variable using the Public keyword makes it available throughout your

application.

 Declaring a local variable using the Static keyword preserves its value even when a

procedure ends.

In VB. NET you don’t have to declare a variable before using it. For example, you could write a

function where you don’t need to declare TempVal before using it:

Function SafeSqr(num)

TempVal = Abs(num)

SafeSqr = Sqr(TempVal)

End Function

Visual Basic automatically creates a variable with that name, which you can use as if you had

explicitly declared it. While this is convenient, it can lead to subtle errors in your code if you

misspell a variable name. For example, suppose that this was the function you wrote:

Function SafeSqr(num)

TempVal = Abs(num)

SafeSqr = Sqr(TemVal)

End Function

At first glance, this looks the same. But because the TempVal variable was misspelled on the

next-to-last line, this function will always return zero. When Visual Basic encounters a new

name, it can’t determine whether you actually meant to implicitly declare a new variable or you

just misspelled an existing variable name, so it creates a new variable with that name.

To avoid the problem of misnaming variables, you can stipulate that Visual Basic always

warn you whenever it encounters a name not declared explicitly as a variable. To explicitly

declare variables, place this statement in the Declarations section of a class, form, or standard module:

Option Explicit or From the Tools menu, choose Options, click the Editor tab and check the Require Variable Declaration option. This automatically inserts the Option Explicit statement in any new modules, but not in modules already created; therefore, you must manually add Option Explicit to any existing modules within a project.

Had this statement been in effect for the form or standard module containing the SafeSqr function, Pind Visual Basic would have recognized TempVal and TemVal as undeclared variables and generated errors for both of them. You could then explicitly declare TempVal:

Function SafeSqr(num)

Dim TempVal

TempVal = Abs(num)

SafeSqr = Sqr(TemVal)

End Function

Now you’d understand the problem immediately because Visual Basic would display an error message for the incorrectly spelled TemVal. Because the Option Explicit statement helps you catch these kinds of errors, it’s a good idea to use it with all your code.

Advertisements