Vous êtes sur la page 1sur 5

Data Types of

Visual Basics 6.0


and its examples
Assignment no.2
SUBMITTED BY : HUSSAIN HAKIM ALI KHAN
SUBMISSION TO: SIR ASIM IFTIKHAR
DATE OF SUBMISSION: 22 OCTOBER 2013
ID CARD #: 2011-1-92-13523
SECTION: A

10/22/2013

Visual Basic/Data Types with examples


Data types in Visual Basic can be divided into three groups:

Native: Types that are understood directly by the Visual Basic compiler without assistance from the
programmer

User-defined: commonly referred to by the initials UDT, meaning User defined Type, these correspond
to Pascal records or C structs

Classes: the basis for object oriented programming in Visual Basic. Classes include forms, add-ins,
and database designers.

Built in Types
The built in types are:
Byte
8 bit, unsigned
Integer
16 bit, signed
Long
32 bit signed
Single
32 bit floating point, range about
Double
64 bit IEEE floating point, range about
Currency
exact representation of decimal numbers of up to four decimal places
String
dynamically allocated UniCode strings, theorectical capacity about
Collection
an associative array of Variants.
Date
8 byte date/time value range January 1, 100 to December 31, 9999
Object
a holder for any type of Object.

characters.

Variant
a holder for any type of value or object.

Byte, Integer & Long


Example:
Dim a as Byte
Dim i as Integer
Dim x,y as Long 'Define two variables. Note that only the last variable will be a
long integer.
Now those variables will only be capable of storing integer values (without decimal). Long integers can store a
number with a bigger range of value than integers but they occupy a bigger space of RAM.

Type Storage

Byte

1 byte

Range of Values

0 to 255

Integer 2 bytes -32,768 to 32,767

Long

4 bytes -2,147,483,648 to 2,147,483,647

Single & Double

These data

Type

Storage

store
values.

decimal

Single

Range of Values

compared to

Round()
decimal to a

"Double"
"Single" is

similar to
compared to

types can

-3.402823E+38 to -1.401298E-45 for negative values


Double

the "Long"
"Integer".

1.401298E-45 to 3.402823E+38 for positive values.


-1.79769313486232e+308 to -4.94065645841247E-324 for negative values
4.94065645841247E-324 to 1.79769313486232e+308 for positive values.

rounds off a
certain

number of decimal digits that the programmer wants. The first argument should be a decimal value which you
want to round off. The second argument specifies the number of decimal digits you want, for example:
Dim pi as Double
pi=3.141592653589
pi=Round(pi,2) 'Rounds off 3.141592653589 to only two decimal digits
Print pi 'Prints 3.14

String
A string is an array of characters. As an example:
Dim a As String
a = "This is a string"
Strings can be concatenated (connected together to form a new string) using the "&" operator. For example,
dim b as String
b = "Wiki" & "book" & "s"
Print b 'Prints "Wikibooks"
A normal string variable occupies 10 bytes of RAM, plus the string's size, and can hold up to 2 billion
characters!

Structure
An example definition of a structured type:
Type E2Point
x As Double
y As Double
End Type
Sub Test()
Dim MyPoint As E2Point
Dim MyPoint2 As E2Point
MyPoint.x = 4
MyPoint.y = -5
MyPoint2 = MyPoint 'Make a copy
MyPoint2.x = 3
MyPoint2.y = -6
Debug.Print MyPoint.x, MyPoint.y '4, -5
not overriden with 3 and -6
Debug.Print TypeOf MyPoint Is E2Point 'True
Debug.Print TypeOf MyPoint Is Object 'False
End Sub
The type has to be defined outside of a procedure.

A variable of a structure type is not an object.

Enumeration
An example definition of an enumerated type:
Enum Colors
Red '=0
Green '=1
Blue '=2
End Enum
Enum Colors2
Red2 = 1
Green2 '=2
Blue2 '=3
End Enum
Sub Test()
Debug.Print Red, Green, Blue
Debug.Print Red2, Green2, Blue2
Dim MyVar As Colors 'Ends up being typed as Long
MyVar = 8 'Does not lead to an error: no restriction on values
End Sub