0% found this document useful (0 votes)
57 views20 pages

CCP503

This document discusses variable scope, lifetime, and types in Visual Basic programming. It defines local variables as those declared within a procedure using Dim, while public variables are declared globally and accessible anywhere. Variable lifetime depends on how they are declared - local variables exist only within a procedure, while public variables last the entire application runtime. The document also covers static variables, user-defined types, constants, and provides examples to illustrate various concepts.

Uploaded by

api-3849444
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views20 pages

CCP503

This document discusses variable scope, lifetime, and types in Visual Basic programming. It defines local variables as those declared within a procedure using Dim, while public variables are declared globally and accessible anywhere. Variable lifetime depends on how they are declared - local variables exist only within a procedure, while public variables last the entire application runtime. The document also covers static variables, user-defined types, constants, and provides examples to illustrate various concepts.

Uploaded by

api-3849444
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 20

DEPARTMENT OF TECHNICAL EDUCATION

ANDHRA PRADESH
Name : K SREE RAMACHARYULU
Designation : LECTURER
Branch : Commercial & Comp. Practice
Institute : Govt. Polytechnic,
SRIKAKULAM
Year / Semester :V
Subject : Visual Basic-I
Subject Code : CCP-503
Duration : 50 Minuets
Topic : Programming Fundamentals
Sub. Topic : VB Environment, Basics of VB
Programming Fundamentals
Teaching Aids : PPT, Animation Clips & Images
CCP503.24 1
Objective :

On completion of this period you would be able to


learn
 The scope of a variable
 Lifetime of a Variable
 How to make use of variables in VB Program

CCP503.24 2
Structure

 Scope of variable

 Life time variable

 Example

CCP503.24 3
Recap
 In the previous session, you have learnt about

 Types of Variables

 Numeric Variables

 Non-numeric Variables

 Variant type of variables

 Object type of variables

CCP503.24 4
Types of variables supported by VB

The VB recognizes the following six data types:


2. Numeric Variables
3. String Variables
4. Boolean Variables
5. Date Variable
6. Object Variables
7. Variant Variables

CCP503.24 5
Visual Basic Variable Scope
 The scope of variable is the section of the application
that can access the variable.
 Where a variable can be accessed depends on how it is
declared
 Local scope (Dim) – only within Procedure (Sub or
Function) in which it is declared
 Module Scope (Private) – available in every Procedure
within a module
 Global Scope (Public) – available throughout application
Controlling scope makes it possible to work within
contexts, so that variable names can be reused
with no conflicts

CCP503.24 6
Decides scope of VB Declarations

Dim index As integer Can be placed anywhere


Private name As String Declarations in General
section Only accessible from
same module
Public today As Date Declarations in General
section Accessible anywhere

CCP503.24 7
Visual Basic Module Structure

Every module has


 General Declarations section
 Subroutines (Subs) and Functions
Form modules also have
 User interface elements (which appear on-screen)
 Event handlers
Class modules also have
 Event receptors (objects that can respond to events)

CCP503.24 8
Lifetime of the variable

 Life time is the period for which variable retains its value.

 Variables declared as public exists for the life time of the


application.

 Local variables, declared with in procedures with Dim


statement live as long as the procedure is executing.

CCP503.24 9
Static variable

 When the procedure finishes, the local variables


cease to exist and the allocated memory is
returned to the system.

 We can force a local variable to preserve its


value between procedure calls with the static
keyword.

CCP503.24 10
VB Local & Static Variables
Local variables (within
Sub or Function)
 Declared with Dim are Sub Forgettable( )
reset to zero every time Dim Number As Integer
Procedure is called Number = Number + 1
End Sub Always 1
 Declared with Static, at End Sub
retain their value
Sub Persistent()
Dim CallCount As
Integer
CallCount = CallCount +
1
End Sub Counts
up

CCP503.24 11
Visual Basic Variant Type
A Variant is an all purpose variable
 Type is always Variant
 Sub-Type matches whatever value is assigned
 Sub-Type changes to suit

Private Sub SquareRoot_Click()


Dim V As Variant
V=2 Assign an integer
V = Sqr(V) Assign a double
V = “Root 2 = “ & V Assign a
string
End Sub

CCP503.24 12
VB User Defined Types
 A UDT is composed of simple types
(Integers, strings etc.)
 Each instance contains one of each
component variable
 Must be placed in Standard Modules
 Definition is Global (available
throughout program)
 Useful for grouping related information

CCP503.24 13
Visual Basic - An example User Defined
(UDT)
Type Phone Entry
Name As String
Telephone As String
End Type
Dim PE As Phone Entry
PE.Name = “Fred Bloggs”
PE.Telephone = “2468”
Print PE.Name,
PE.Telephone

CCP503.24 14
Constants in VB

 Variables that does’t change its value during


the execution of the program are called
Constants.
Eg. Const Pi as double=3.14159
Dim Area, r as Double
Area= 2*Pi*r

where r is the value of the radius of the circle


is to be given by the user.
CCP503.24 15
Summary

 The Scope & Life time of variables in the VB


Programming

 The module structure in a VB Program

 the nature of the Static variable and user defined


Variables, Constants in VB Program.

CCP503.24 16
Quiz
1. What is a local Variable ?

A) Declared in a Application
B) Declared in a Procedure
C) Declared in the module
D) Declared in the form level

CCP503.24 17
1. What is user defined variable?

A. Defined by the End user


B. Defined by the programmer
C. Defined by the Customer
D. Defined by the Administrator

CCP503.24 18
Quiz

3. What is a public variable?

A. Defined in the Options part


B. Defined in code window
C. Defined in module
D. Defined in a procedure

CCP503.24 19
Frequently asked questions
1. Explain in brief about the scope of variables in
Visual Basic
2. Briefly explain about the lifetime of a variable with
one example?
3. What is user defined variable? Give an example.

CCP503.24 20

You might also like