Home

Contents

Web-Based Training

Research

VB

 

Arguments and Data Types in Visual Basic

 

-----------------------------------------------------------------------

Index of Page Topics

Arguments

Visual Basic

Data Types

Problem Solving

-----------------------------------------------

Arguments

Procedures are the building blocks of a program and are aggregated to form modules. They themselves are composed of several different parts, namely:

    1. A two-line format that identifies the beginning, the end, and the type of the procedure.
    2. The name of the procedure.
    3. The collection of statements in the body of the procedure.
    4. Declaration of the arguments.
    5. Specification of the scope of individual variables.
    6. Specification of the lifetime of individual variables.
    7. Specification of the type of argument transfer.
    8. Identification of the data types used in the arguments.

Click here to review procedures. In this section we look more closely at the arguments used by the procedures. We examine items 4 through 8 in the above list of parts.

An argument evaluates a procedure. In math terms, it is an independent variable of a function. But now it applies not only to Function procedures, which are numerical, but also to Sub and Property procedures, whose variables may be alphanumerics. An important difference is that Function procedures return numerical values, but Sub procedures do not. On the other hand, Property procedures can return values, and they can also assign values and set references to objects. There are, in fact, several data types, as we will see. So you can think of an argument as a generalized variable.

 

Declaring Arguments

In Visual Basic it isn't necessary to declare a variable explicitly, but it's good practice. It can save problems later in case you misrepresent the variable in some way, say by misspelling it and inadvertently creating a "new" variable. You declare it implicitly if you simply use it where you need the variable. For instance, in error you might write:

lenth = 37

By stating 'length' explicitly before you apply it, Visual Basic alerts you to the presence of the unwanted stranger 'lenth,' should you make that mistake.

As in procedure formats, you identify the data type with the key words As type, and you declare the variable with the Dim keyword:

Dim variable As type

Scope and Duration of Arguments

If a declaration is made inside a procedure, the variable is understood to be strictly local to the procedure (or private) and it exists only as long as the procedure is being executed.

You can make it accessible from outside the procedure by declaring it in other ways. For one thing, you can declare it in the Declarations section of a form, standard or class. You can also declare it using the keyword Public. And you can do so with the Static keyword. These options make the variable available at all times from anywhere in the application.

 

Passing Arguments to Procedures

Passing an argument to a procedure is simply the transfer of information to the procedure that needs it to do its work, like providing values for the independent variables of a function. The transfer is made at the time the procedure is called, and there are two ways to specify how the transfer of information is to be made. In passing arguments by value, indicated by the keyword By Val, only a copy of an argument, or variable, is passed. Should the procedure change the argument's value, only the copy is affected; the variable itself, from which a copy is made, is left unchanged. In other words, use of By Val preserves the value of the variable.

The other option is to transfer the actual contents of the variable, rather than a copy. This is done by using the keyword By Ref, the possible effect of which is to permanently change the variable. This is the default condition in Visual Basic, when the type of transfer isn't specified.

Back to Index

 

-----------------------------------------------

Data Types

Thinking of a variable as a holding place for data, a generalized variable can be seen as a repository for a wide range of data types, which might include objects, strings, or Boolean data, as well as ordinary numbers, among other things. And all of the types may be handled in arrays. In Visual Basic, you can specify a data type when you declare a variable, or you can leave the type unspecified, which renders it Variant, by default, a catch-all category.

If you adopt the latter, Visual Basic performs the necessary conversions to deal with the different types. Just be sure to be consistent! For instance, use numbers when doing ordinary arithmetic, or strings when concatenating strings, and use the appropriate operators, like + for adding numbers, and & for "adding" strings. In other words, be consistent.

As provided by Microsoft's Visual Basic 4.0 documentation, a summary of the data types is given in the following table.

Type

Storage

Range

Integer

Long

2 bytes

4 bytes

-32,768 to 32,767

-2,147,483,648 to 2,147,483,647

Single

4 bytes

-3.402823E38 to -1.401298E-45 (neg.)

1.401298E-45 to 3.402823E38 (pos.)

Double

8 bytes

-1.79769313486231E308 to

-4.94065645841247E-324 (neg.)

4.94065645841247E-324 to

1.79769313486231E308 to (pos.)

Currency

 8 bytes

-922337203685477.5808 to

922337203685477.5807

String

1 byte per

character

0 to about 65,500 characters

0 to 2E32 on 32-bit systems

Byte

1 byte

0 to 255

Boolean

2 bytes

True or False

Date

8 bytes

Jan 1, 100 to Dec 31, 9999

Object

4 bytes

Any Object reference

Variant

16 bytes + 1 byte for each character

Null, Error, any number up to the range of a Double, or any character text, object, or array.

Back to Index

------------------------------------------

Top of Page