Sie sind auf Seite 1von 26

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 and Images
CCP503.23 1
Objectives
On completion of this period, you would be able to
understand about

 Different Types of variables supported by VB

How to use variables in VB Program

Numeric Variables Types

Non-Numeric types of variables

CCP503.23 2
Structure

 Types of Variables – Classification

 Range of Variables

CCP503.23 3
Recap

In the previous session, you have learnt about …

 Definition of Variable

 The primary classification of variables

 Declaration of variables

CCP503.23 4
Different types of variables supported
by VB & Their classification
The VB recognizes the following six types of
variables:
1. Numeric Variables
2. String Variables
3. Boolean Variables
4. Date Variable
5. Object Variables
6. Variant Variable

CCP503.23 5
Classification of Variables in brief

Integer types (Byte, Integer, Long) for whole numbers

Floating point types (Single, Double) for numbers with


fractional parts

Dates and times (one type, Date, for both)

Fixed point types (Decimal, Currency) for high


precision (many digits)

String type for text


CCP503.23 6
Numeric Data Type
 They consists of numbers

 Can be computed mathematically with various


standard operators like add, minus, multiply, divide
and so on.

 Divided into 7 types & can be summarized in following


table

The VB recognizes the following ranges of Numeric Variables

CCP503.23 7
Numeric Variables and its Range of Values

Type Stoage Range of Values


Byte 1 byte 0 to 255
Integer 2 bytes -32,768 to 32,767
-2,147,483,648 to
Long 4 bytes
2,147,483,648

CCP503.23 8
Numeric Variables and its Range of Values

Stoage
Type Range of Values

Single 4byte -3.402823E+308to -1.401298E-45 to 3.402823E+38 for positive values

Currency 8 bytes -922,337,203,685,477.5808 to 922,337,203,685,477.5807

+/- 79,228,162,514,264,337,593,543,950,335 if no decimal is use


Decimal 12 bytes
+/- 7.9228162514264337593543950335 (28 decimal places).

CCP503.23 9
General syntax of the Variable declaration
In order to declare any type of variable the following syntax
is used
 Dim <variable Name> as <variable data type>

Examples:
 Dim count as integer
 Dim Days In Country as long
 Dim length as single
 Dim area as double
 Dim volume as double
 Dim area as double, count as integer (multiple
declaration)
CCP503.23 10
Non Numeric Data Type

Data Type Storage Range


String(
Length of
fixed 1 to 65,400 characters
string
length)
String(vari
Length +
able 0 to 2 billion characters
10 bytes
length) January 1, 100 to December 31,
Date 8 bytes
9999
Boolean 2 bytes True or False

CCP503.23 11
Non Numeric Data Type

Data Type Storage Range

Object 4 bytes Any embedded object


Variant(nu
16 bytes Any value as large as Double
meric)
Variant(tex Length+22
Same as variable-length string
t) bytes

CCP503.23 12
String Data Type
 This data type holds only text

 Their declaration is as follows


 Dim statement as string

 We can declare the size of string in the declaration itself


 Dim statement as string 60

 If we assign a string that exceeds the maximum declared


length, it will be truncated

CCP503.23 13
Boolean Data Type
 It stores True/False values
 VB allocates 2bytes to this data type
 They are,in essence,integers that take the value –
1 (for True) and 0 ( for False)
 Examples:

Dim failure as Boolean


if failure then msgbox ( “couldn’t complete the operation”)
 By default Boolean variables are initialized to False

CCP503.23 14
Date Data Type
 Date & time values are stored internally in a special format,
but we don’t need to know the exact format

 These are double precision numbers

 The integer part represents the date and fractional part


represents the time

CCP503.23 15
Date Data Type

 Example:
dim expiration as date
expiration = “21/02/2008”
expiration = “22:30:15 PM”
expiration = “21/02/2008 22:30:15PM”
expiration = “#21/02/2008 22:30:15PM#”

CCP503.23 16
Variant Data Type
 It is more flexible type

 Can hold all other types integer as well as strings, i.e.


any type

 Use dim statement without specifying a type, as follows:


 Dim myvar

 We can also specify the variant type to make the code


clear
 Dim myvar as variant

CCP503.23 17
Byte Data Type
 Actually none of numeric types stored in a single
byte
 Sometimes, data is stored as bytes, and we must be
able to access individual bytes
 Byte type holds basically an integer in range 0 to 255

 They are frequently used to access binary files,


image and sound files, and so on.

CCP503.23 18
Byte Data Type

 Some Windows API calls also use Byte


arguments
dim n as byte
dim a as byte , b as byte
a=233 ; b=50

CCP503.23 19
Object Data Type
 An Object variable refers to one of the VB’s objects

 We can use an object variable to access actual object

Example:
 A form has two command buttons on it,
command1 & command2.
 We can declare two object variables as
Dim a as commandbutton
Dim b as commandbutton

CCP503.23 20
Continuation…

 Each of two object variables can be set to either one of


the command buttons with following code
set a =command1
Set b = command2

 We can use caption of command of button as follows


a.caption = “hello”

 We can turn on Bold attribute of 2nd button as following


b.fontbold = True

CCP503.23 21
Summary

In this Session, we have learnt …


 Types of Variable
 Numeric Variables
 Non-numeric Variables
 Variant type of variables

CCP503.23 22
Quiz
1. The storage size of the single data type is --------

C. One Byte
D. Two Bytes
E. Three Bytes
F. Four Bytes

CCP503.23 23
Quiz continuation
2. If we assign text exceeding max Length to a
string data type

A. Excess text will be truncated

C. All the text will be displayed

E. No text will be displayed

G. Displays error message

CCP503.23 24
Quiz
3 Boolean data type holds by default

A. True
C. False
E. Null
G. 1

CCP503.23 25
Frequently Asked Questions
1. List the different types of Numeric variables?

3. List and explain various data types in VB

5. What is Boolean Variable?

4. What is string variable? Give one example.

CCP503.23 26

Das könnte Ihnen auch gefallen