Managing Visual Basic Data
Managing Visual Basic Data
Managing Visual Basic Data
There are many types of data that we come across in our daily life. For example, we need to
handle data such as names, addresses, money, date, stock quotes, statistics and more everyday.
Similarly in Visual Basic, we have to deal with all sorts of of data, some can be mathematically
calculated while some are in the form of text or other forms. VB divides data into different types
so that they are easier to manage when we need to write the code involving those data.
5.1 Visual Basic Data Types
Visual Basic classifies the information mentioned above into two major data types, they are the
numeric data types and the non-numeric data types.
5.1.1 Numeric Data Types
Numeric data types are types of data that consist of numbers, which can be computed
mathematically with various standard operators such as add, minus, multiply, divide and more.
Examples of numeric data types are examination marks, height, weight, the number of students
in a class, share values, price of goods, monthly bills, fees and others.
In Visual Basic, numeric data are divided into 7 types, depending on the range of values they can
store. Calculations that only involve round figures or data that does not need precision can use
Integer or Long integer in the computation. Programs that require high precision calculation need
to use Single and Double decision data types, they are also called floating point numbers. For
currency calculation , you can use the currency data types. Lastly, if even more precision is
required to perform calculations that involve a many decimal points, we can use the decimal data
types. These data types summarized in Table 5.1
Table 5.1: Numeric Data Types
String(fixed length)
Storage
Range
0 to 2 billion characters
bytes
Date
8 bytes
Boolean
2 bytes
True or False
Object
4 bytes
Variant(numeric)
16 bytes
Variant(text)
Suffix
Data Type
&
Long
Single
Double
Currency
In addition, we need to enclose string literals within two quotations and date and time literals
within two # sign. Strings can contain any characters, including numbers. The following are few
examples:
memberName="Turban, John."
TelNumber="1800-900-888-777"
LastDay=#31-Dec-00#
ExpTime=#12:00 am#
No spacing is allowed
Examples of valid and invalid variable names are displayed in Table 5.4
Table 5.4
Valid Name
Invalid Name
My_Car
My.Car
ThisYear
1NewBoy
Long_Name_Can_beUSE He&HisFather
*& is
not acceptable
5.2.2 Declaring Variables Explicitly
In Visual Basic, it is a good practice to declare the variables before using them by assigning
names and data types. They are normally declared in the general section of the codes' windows
using the Dim statement. You can use any variable to hold any data , but different types of
variables are designed to work efficiently with different data types .
The syantax is as follows:
Dim VariableName As DataType
If you want to declare more variables, you can declare them in separate lines or you may also
combine more in one line , separating each variable with a comma, as follows:
Dim VariableName1 As DataType1, VariableName2 As DataType2,VariableName3 As
DataType3
Example 5.1
After declaring various variables using the Dim statements, we can assign values to those
variables. The general format of an assignment is
Variable=Expression
The variable can be a declared variable or a control property value. The expression could be a
mathematical expression, a number, a string, a Boolean value (true or false) and more. The
following are some examples:
firstNumber=100
secondNumber=firstNumber-99
userName="John Lyan"
userpass.Text = password
Label1.Visible = True
Command1.Visible = false
Label4.Caption = textbox1.Text
ThirdNumber = Val(usernum1.Text)
total = firstNumber + secondNumber+ThirdNumber
To compute inputs from users and to generate results, we need to use various mathematical
operators. In Visual Basic, except for + and -, the symbols for the operators are different from
normal mathematical operators, as shown in Table 6.1.
Table 6.1: Arithmetic Operators
Operat
or
Mathematical function
Example
Exponential
2^4=16
Multiplication
4*3=12,
Division
12/4=3
15 Mod 4=3
19\4=4
Mod
\
"Visual"&"Basic"="Visual
Basic"
Example 6.1
In the Example 6.2, three variables are declared as integer and two variables are declared as
variant. Variant means the variable can hold any data type. The program computes the total and
average of the three numbers that are entered into three text boxes.
Conditional Operators
To control the VB program flow, we can use various conditional operators. Basically, they
resemble mathematical operators. Conditional operators are very powerful tools, they let the VB
program compare data values and then decide what action to take, whether to execute a program
or terminate the program and more. These operators are shown in Table 7.1.
Table 7.1: Conditional Operators
Operator
Meaning
Equal to
>
More than
<
Less Than
>=
<=
<>
Not Equal to
In addition to conditional operators, there are a few logical operators that offer added power to
the VB programs. They are shown in Table 7.2.
Table 7.2:Logical Operators
Operator
And
or
Meaning
Xor
Not
Negates truth
* You can also compare strings with the operators. However, there are certain rules to follow
where upper case letters are less than lowercase letters, and number are less than letters.