Visual Basic

This page contains descriptions of the main structures and data types used when programming in Visual Basic. To suggest changes go to the Contact Me page. 

I am using Visual Basic 5.0, which isn't the latest version of the language. There are undoubtedly changes in VB6 that I am not aware of.


Contents...

Introduction

Data Types

Constants

Enumerations

Types

Arrays

If-Then-Else

While Structures

For Structure

Sub Procedures

Function Procedures

Property Procedures

Modules

 

Introduction

Visual Basic is a highly popular language in the commercial world because it allows for the rapid development of Windows based programs. VB is particularly strong at creating front ends for databases. This can be done in amazing time through the use of wizards. This page does not cover all aspects of VB, it does not show how to do the basics like layout a form, neither does it cover all the built in functions, as there is already plenty of help provided for these, and a lot of it is self-evident. 

A more limited version of Visual Basic is also included in several other Microsoft Applications such as MS Access. Most of the information here applies to that version.

Their is also VB Script for creating web pages. Much of the information on this page applies, but VB Script only has one basic data type - the Variant type.

Data Types

- Byte : 1 Byte

- Integer : 4 Byte (on 32 bit) : %

- Long : 4 Bytes : &

- Single : 7 Digits : !

- Double : 16 Digits : #

- String

- Boolean (b As Boolean / b = true, false)

- Date (d As Date / d = #01/01/2000)

- Currency

- Variant

Constants

Constants are declared as follows:

Const name [As Type] = value

eg. Const i As Integer = 10

Enumerations

Enumerations are declared as follows:

Public Enum X

    Value1

    Value2

End Enum

dim e As X

e = Value1

Types (records)

Types are created along the lines of the following example:

Public Type Person

Name As String

Age As Integer

End Type

Dim mark As Person

mark.Name = "Mark"

Arrays

Fixed size arrays are created as follows:

Dim initarray(10) As Integer

this will create an array of 10 elements starting at 0

Alternatively you can state the starting number

Dim initarray(10 To 19) As Integer

Elements can then be used as follows:

initarray(0) = 10

Two dimensional arrays are created as follows:

twodimension(10, 10) As Integer

They are used as follows:

twodimension(0, 0) = 10

Arrays can also be made dynamic. The first step is to declare an array as follows:

expandable() As Integer

It can then be given 'x' number of elements as follows:

ReDim expandable(x)

To increase the array another time while keeping the current elements use the following structure:

Redim Preserve expandable(y)

If-Then-Else

The basic structure is as follows:

If condition1 Then

statements

Else If condition2 Then

statements

Else

statements

End If

The Else If portion is optional, as is the Else part.

The Case structure goes as follows:

Dim x As Integer

Select Case x

Case 0

statement

Case 1

statement

Case Else

statement

End Select

While Structures

There are two while structures. The main one is:

Do While condition

statements

Loop

This will not necessarily execute.

The other one, which will always execute once is:

Do

statements

Loop While condition

To exit from these loops prematurely use:

Exit Do

For Structure

The For loop structure is as follows:

For condition = x To x [Step x]

statements

Next

To exit from this loop prematurely use:

Exit For

Sub Procedures

Sub procedures are created with the following structure:

[Static] Sub name (Argument List)

    statements

End Sub

For instance:

Sub printMessage(str As String)

MsgBox(str)

End Sub

This procedure is called as follows:

- From inside the same module : printMessage str

                                           or : Call printMessage(str)

- From a different module : moduleName.printMessage(str)

Arguments can also be passed by value:

Public Sub printMessage(ByVal str As String)

Arguments can be made optional:

Sub printMessage(str1 As String, Optional str2 As String)

These optional arguments can be given default values:

Sub printMessage(str1 As String, Optional str2 As String = "Hello World")

Or we can give them values when we call them with the := operator:

printMessage str2:= "hello world"

This allows optional arguments in the argument list to be skipped

Function Procedures

Functions, which can return a value, have the following structure:

[Static] Function name(argument list) [As return value]

statements

End Function

For instance:

Function cube(number As Integer) As Integer

cube = number *number * number

End Function

Functions are called as follows:

value = cube(5)

or

cube 5

Property Procedured

These are used to set the values of private module variables:

Property Get procedureName () As return value

procedureName = varaiable

End Property

They can also take the form

Property Let procedureName (value As type)

Modules

Once Classes/Modules have been created with a .cls extension they are declared and used as follows:

Dim c1 As classname

Set c1 = New classname

The constructors and destructors are in the following procedures:

Private Sub Class_Initialize()

Private Sub Class_Terminate()

Classes can be destroyed as follows

Set c1 = Nothing


Wednesday, February 23, 2000