You are on page 1of 24

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.22 1
Objectives
On completion of this period you would be able to
understand
 Definition of the variable

 How to declare variable

 Types of Variables
Manipulation of Variables

CCP503.22 2
Structure
 Variable - Definition
 Declaring variable - Types
 Examples for Variable declaration
 Naming conventions of the variables

CCP503.22 3
Recap

 In the previous lesson you have learnt the abilities of

VB IDE, Objects and Events, etc,.

 Fundamentals of procedural programming capabilities.

 Event handling capabilities

CCP503.22 4
Introduction to Variables
 In any Programming Language variables are
used to store data values during a programme
execution temporarily.

 Declaration tells the V.B how much amount of


memory is required?

CCP503.22 5
Variable Definition
 Variable is a placeholder used to store data values and recall
them at will
 Variables are stored values during a program’s execution.
 It has a name and a value.
 They have names & Data Types
 The Data type of a variable determine how the bits
representing those values are stored in system memory

CCP503.22 6
Example for string data type: -

A Variable has a name & a value


 Eg. Dim a as Integer.
Dim b
Dim Name as String
 Ex1. Dim Name as string

Name = “Hari”

Here Name of the variable is name and data value is “HARI”

of string data types

CCP503.22 7
Example for string data type: -
 Ex2. Dim age as integer
Age=20
Here name of the variable is age and the data value
is 20 of the integer data type.

CCP503.22 8
Declaring Variables
In most of the languages, variables must be
declared in advance for the compiler

 Because every time a compiled application runs into a


new variable.
 If it is not declared prior, the compiler has to create it
which consumes a lot of time

 If compiler knows all variables & their types before


execution, it produces compact and efficient code

CCP503.22 9
Declaring Variables
 Examples:
1.Dim meter as integer.
2.Dim var1 as variant.

CCP503.22 10
Types of Variable Declaration
Basically two types
2) Explicit Declaration
3) Implicit Declaration
 In general we have to use Dim statement which is a keyword
followed by the variable’s name and the data type
 Examples:
 Dim meters as integer
 Dim greetings as string

CCP503.22 11
Key Word
 A keyword is also called reserve word which is
used to its intended purpose only.

 Dim is used to declare a variable only not


other than that purpose.

CCP503.22 12
1. Explicit Declaration
Here all variables must be declared before
they are used
For Example:
4. Dim Marks as Integer
5. Dim name as String
6. Dim total as Integer
7. Dim Age as Integer

CCP503.22 13
Option Explicit variable Declaration
 We can find this type by the following
statement:

 When VB finds Dim statement, it creates one


or more new variables

 It creates placeholder by reserving some


space in memory and assigning a name to it

CCP503.22 14
Different Examples
Dim meter as integer
Dim greetings as string
Meter=100
Greetings=“Welcome to Visual Basic”

 compiler identifies the “meter” is a variable name and


holds any integer value & not able to store other data
type values

 Similarly the greetings is a variable that holds string


value and not able to store any other data type

CCP503.22 15
2.Implicit Declaration
 Here it is not compulsory to specify the data type of
the variable
 When VB encounters an undeclared variable on the
spot and uses it automatically.

 In other words when VB encounter specified


data type of the variable :

 It creates a new variable on the spot and uses it


 The new variable type is variant
 Variant is the generic data type that can
accommodate all other data types

CCP503.22 16
Examples of Implicit Declaration
Examples:
Dim Name1, var1
Name1 =“Krishna kumar”
Var1 = 123
Here var1 is string type and var2 is integer type
This can be verified by using Type Name()
function,returns variable type
 Some more examples
Dim Marks,
Dim Name,
Dim Tot_bal,
Dim Age
CCP503.22 17
Primary Classification of Variables
Basically Variables are classified into 2 types
 Numeric
 Non Numeric
Under Non-Numeric
 String
 Date
 Boolean
 Object
 variant

CCP503.22 18
Variable Naming Conventions

When you are going to declare any variable,


the following naming conventions should be
followed:
3) First character must begin with an alphabet
4) Can’t contain an embedded period or any of the
type-declaration’s characters
5) Should not exceed 255 characters
6) Must be unique with its scope

CCP503.22 19
Summary
In this Session, you have learnt about …

 Definition of Variable,

 Declarations of the variables

 Types of Variables

 Naming conventions of the Variables.

CCP503.22 20
Quiz

1.Which one of the following is a Numeric type?

A. Character
B. Byte
C. String
D. Integer

CCP503.22 21
Quiz
2. Which one of the following case is a NOT a valid

declaration ?

 Implicit Declaration
 Explicit Declaration
 Name as Integer
 Dim A

CCP503.22 22
Quiz

3. Which data type can hold any type of data?


A. Long
B. Byte
C. Variant
D. String

CCP503.22 23
Frequently Asked Questions

1. What is variable?

3. List and explain the types of variable declaration?

5. List any four data types in VB?

7. Write about variables, declaration and their and types?

9. Briefly explain the variable naming conventions in VB.

CCP503.22 24

You might also like