VB6 Tutorial 07: Variable Scopes

Posted On 2018-03-23 by VB6Boy
Keywords: visual,basic,vb6,visualbasic,tutorial,basic,beginner,Event,Variables,Data,Types,scopes
Tags: VB6 Tutorials Windows
Views: 54


Looking for the more of the VB6 Tutorial? Here is the tutorial index.

This lesson will cover scoping and lifetime of variables in procedures and modules.

What is scope?

Scope of a variable determines which part of the code can access the variable. A variable is declared in general declaration section of a module to make it available to all the procedures in the module.

There are two levels in which you can define variables:

  • Procedure level
  • Module Level

Example

The following block of code is a procedure.

Private Sub Command1_Click()
    Dim num As Integer
    num = 45
    Print num
End Sub

'Command1_Click()' is the procedure name. Here 'num' is a procedure-level variable. The value of the variable is destroyed when the procedure ends.

'End Sub' indicates the end of the event procedure.

What is a procedure?

Procedure is a block of code that is executed as a unit.

Types of procedure

There are three types of procedures.

  1. Sub procedure
  2. Function procedure
  3. property procedure

Sub procedures are of two types:

  1. General procedure
  2. Event procedure

Modules

Visual Basic uses modules. The three module types are:

  1. Form Modules
  2. Standard Modules
  3. Class Modules

Form Module

Combination of procedures in a form is referred to as form module.

Standard Module: (.bas extension)

Go to

Menu -> Project -> add module
to add a module to your application. This type of module is used for a common code to be executed in several forms or all forms.

QuickHint:

Form module is used for the execution of a common code repeatedly only in that form. But standard modules can be used for the repeated execution of a common code in all forms.

Class Module (.cls extension)

Class module is used for object oriented programming.

Here procedures and modules have been discussed very briefly. A complete chapter is dedicated on these topics.

Procedure Level Variable or Local Variable

Local variables are of two types - Dynamic local variable and Static local variable.

Dynamic local variable

A Dynamic local variable is declared inside a procedure using the dim keyword. Local variables are only available to the procedure in which they are declared. Other parts of the code are unaware of its existence. Values of the variables declared using the dim keyword in a procedure exist only within the procedure.

Example

Private Sub Command1_Click()
    Dim num As Integer  'This is a dynamic local variable
End Sub

Static Variable

Static variable is a procedure level variable.

Syntax

static variable as [type]

Example

static m as integer

Static variables are declared inside a procedure and it retains its value even when a procedure ends but the scope is the procedure itself.

Module level variable

By default, a module level variable is accessible from all the procedures in the module but not from other modules. A module level variable is declared in the Declarations section, at the top of the module using the Dim or Private keyword. There is no difference between Dim and Private at the module level.

Example

'in the Declarations Section
Private s As String

Public Variable or Global variable

To make a variable accessible to all the modules or throughout the application, declare it with the Public keyword in the Declarations section of a module. Then the value of the variable becomes available to all the procedures of your application.

QuickHint:

You cannot declare a public variable inside a procedure, public variables are declared only within the Declarations section of a module.

Example

'Form1
'in the form's declaration section
Public n As Integer

'Form2
'To access from another form
msgbox Form1.n

'To access a global variable declared in a .BAS module
msgbox Module1.n

The concept of scope and lifetime of a variable is very important. When you will learn a bit more about vb coding, you will understand much in this respect.

Looking for the more of the VB6 Tutorial? Here is the tutorial index.


About the Author

VB6Boy has posted a total of 71 articles.


Comments On This Post

No comments on this post yet!


Do you have a thought relating to this post? You can post your comment here. If you have an unrelated question, you can use the Q&A section to ask it.

Or you can drop a note to the administrators if you're not sure where you should post.


Your IP address is:23.20.165.182

Before you can post, you need to prove you are human. If you log in, this test goes away.




Code Links