Data Types and Operators in VB6.0
VISUAL BASIC 6.0 - VARIABLE, CONSTANT, DATA TYPES and OPERATORS
VARIABLE
A Variable is a named storage locations in memory.
The declared variables can easily be referenced in other places in the application code.
The value of the variable does not change during program execution of a VB application.
Variables can be declared in form, module or global scope and can be public or private.
Naming Rules
Each programmer has to name the variables
1.Name may consist of letters, digits and underscores
2.It must be 1 to 255 characters in length
3.It may not be reserved words (keywords)
General Form
[Scope] Dim Identifier [As Datatype] = value
Example
Dim StuName as String
Dim count as Integer
CONSTANT
Constants are named storage locations in memory.
The value of constant does not change during program execution.
Constants can be declared in form, module or global scope and can be public or private.
General Form
[Scope] Const Identifier [As Datatype] = value
Example
Const PI = 22/7
Public Const gravity As Single = 9.81
Private Const PI As Single = 3.14
Predefined Visual Basic Constants
The predefined constants can be used anywhere in the code and define / assign the default values.
Example
Consider a statement that will set the window state of a form to be maximized.
Form1.Windowstate = 2 (or)
Form1.WindowState = vbMaximized
DATA TYPES IN VB 6.0
The data type of a variable or constant indicates what type of information will be stored in the allocated memory space: perhaps a name, date, age or amount.
When a variable is declared, a data type is assigned for it that determines the kind of data they can store.
By default VB variables are of variant data types which allow storing numeric, date / time or string data.
The fundamental data types in VB include integer, long, single, double, string, currency, byte and boolean.
In addition, some types can interchange with some other types.
A list of VB's data types are given below with minimum and maximum values it can hold
1. Numeric
Byte |
Store integer values in the range of 0 - 255 |
Integer |
Store integer values in the range of (-32,768) - (+ 32,767) |
Long |
Store integer values in the range of (- 2,147,483,468) - (+ 2,147,483,468) |
Single |
Store floating point value in the range of (-3.4x10 -38 ) - (+ 3.4x10 38 ) |
Double |
Store large floating value which exceeding the single data type value |
Currency |
Store monetary values. It supports 4 digits to the right of decimal point and 15 digits to the left |
2. String
Use to store alphanumeric values. A variable length string can store approximately 4 billion characters
3. Date
Use to store date and time values. A variable declared as date type can store both date and time values and it can store date values like mm / dd / yyyy.
4. Boolean
Boolean data types hold either a true or false value. These are not stored as numeric values and cannot be used as such. Values are internally stored as 1 (True) and 0 (False) and any non-zero value is considered as true.
5. Variant
Stores any type of data and is the default VB data type. In VB if we declare a variable without any data type by default the data type is assigned as default.
Here is a list of the most common data types and their prefixes:
Data Type |
Prefix |
Example |
Boolean |
bln |
blnFound |
Byte |
byt |
bytTracks |
Date / Time |
dtm |
dteStartOfShift |
Double |
dbl |
dblDistance |
Error |
err |
errCantOpen |
Integer |
int |
intNbrOfStudents |
Long |
lng |
lngPopulation |
Object |
obj |
objConnection |
Single |
sng |
sngAge |
String |
str |
strCountryName |
Currency |
cur |
curHourlySalary |
Variant |
var |
varFullName |
Example
Private Sub Form_Load()
Dim CountryName As
String
Dim IsMarried As
Boolean
Dim StudentAge As
Byte
Dim Tracks As
Integer
Dim Population As
Long
Dim Distance As
Single
Dim StartingSalary
As Currency
Dim DateOfBirth As
Date
Dim KickOffTime As
Date
End Sub
OPERATORS IN VB
An operator is a symbol that performs some mathematical or
logical operations.
Generally in VB 6.0, there are three types of operators.
Arithmetical Operators
To perform various arithmetic
operations like addition, subtraction, multiplication, division and
exponentiation
Operators |
Description |
Example |
Result |
+ |
Add |
5+5 |
10 |
- |
Subtract |
10-5 |
5 |
/ |
Divide |
25/5 |
5 |
\ |
Integer
Division |
20\3 |
6 |
* |
Multiply |
5*4 |
20 |
^ |
Exponent
(power of) |
3^3 |
27 |
Mod |
Remainder
of division |
20
Mod 6 |
2 |
& |
String
concatenation |
"GOOD"&"
"&"LUCK" |
"GOOD
LUCK" |
Relational
Operators
To form conditions, 6 relational operators are used
to compare values. The result of the
comparison is either True or False.
Operators |
Description |
Example |
Result |
> |
Greater
than |
10>8 |
True |
< |
Less
than |
10<8 |
False |
>= |
Greater
than or equal to |
20>=10 |
True |
<= |
Less
than or equal to |
10<=20 |
True |
<> |
Not
Equal to |
5<>4 |
True |
= |
Equal
to |
5=7 |
False |
Logical Operators
To create compound conditions (to test more than one condition) by using logical
operators, which is always True or False.
Operators |
Description |
OR |
Operation will be true if either of the operands is true |
NOT |
Operation will be true if not equal to the operands is true |
AND |
Operation will be true only if both the operands are true |
Comments
Post a Comment