Unit 04
Unit 04
Unit 4
Managing Data
Introduction
We come across many types of information and data in our daily life. For
example, we need to handle data such as name, address, money, date,
phone no and more every day. Similarly in Visual Basic 2008, we have to
deal with all sorts of data; some can be mathematically calculated while
some are in the form of text or other forms. VB2008 divides data into
different types so that it is easier to manage when we need to write the
code involving those data. In this unit, we‘ll explore the basic data types,
keywords of Visual Basic 2008.
Lesson 4.1
Data types
Upon completion of this unit you will be able to:
Outcomes
Data type
A data type in a programming language is a set of data with values having
predefined characteristics. Usually, a limited number of such data types
come built into a language. The language usually specifies the range of
values for a given data type, how the values are processed by the
computer, and how they are stored. When you develop a program, you
must need to work with different types of data such as numeric, floating
point, Boolean, String, character etc. With object-oriented programming,
a programmer can create new data types to meet application needs. Data
types define particular characteristics of data used in software programs
and inform the compilers about predefined attributes required by specific
variables or associated data objects.
88
Visual Programming
89
Data types
90
Visual Programming
Lesson 4.2
Variables
Upon completion of this unit you will be able to:
Declare variables
Assign values to the variables.
Outcomes
Variable
A variable is nothing but a name given to a storage area that our
programs can manipulate. Each variable in VB has a specific type, which
determines the size and layout of the variable's memory; the range of
values that can be stored within that memory; and the set of operations
that can be applied to the variable.
Note it!
Declaring Variables
In Visual Basic 2008, you need to declare the variables before you can
use them by assigning names and data types. If you fail to do so, the
program will show an error. They are normally declared in the general
section of the codes' windows using the Dim statement. The syntax is as
follows:
Dim Variable Name As Data Type
Example
Dim total As Integer
Dim name As String
Dim doDate As Date
You may also combine them in one line, separating each variable with a
comma, as follows:
Dim total As Integer, name As String, doDate As Date.
91
Variables
Constants
Constants are different from variables in the sense that their values do not
change during the execution of the program. The format to declare a
constant is
Const Constant Name As Data Type = Value
92
Visual Programming
Lesson 4.3
Keywords
Upon completion of this unit you will be able to:
Define Keyword.
Use Keyword.
Outcomes
Keyword
Keyword is predefined, reserved identifier that has special meanings to
the compiler. Visual basic has several keywords. Keywords are used to
perform specific task. A keyword is an essential part of language
definition. They cannot be used as identifiers or variables in your
program.
Types of keywords
Visual basic 2008 provides the following two types of keywords:
Reserved keywords
Unreserved keywords
Reserved keywords
Reserved keywords are those words, these are not be used as
programming elements like variables and procedures. The following table
shows the available reserved keyword in visual basic 2008.
#Const #Else #Elseif #End #EIf
= & &= * *=
/ /= \ \= ^
^= + += = -=
AddHandler AddressOf Alias And AndAlso
As Boolean ByRef Byte ByVal
Call Case Catch CBool CByte
CChar CDate CDec CDbl Char
CInt Class CLng CObj Const
Continue CSbyte CShort CSng CStr
93
Keywords
Unreserved keywords
Unreserved keywords are those words; these are used as programming
elements like variables and procedures. The following table shows the
available reserved keyword in Visual Vasic 2008.
94
Visual Programming
Lesson 4.4
Mathematical Operations
Upon completion of this unit you will be able to:
Outcomes
Mathematical Operations
Computers can perform mathematical calculations much faster than
human beings do. However, computer itself is not able to do any
mathematical calculations without receiving instructions from the user. In
VB2008, we can write code to instruct the computer to perform
mathematical calculations like addition, subtraction, multiplication,
division and other kinds of arithmetic operations. VB2008 arithmetic
operators are very similar to the normal arithmetic operators, only with
slight variations. The plus and minus operators are the same while the
multiplication operator use the * symbol and the division operator use the
/ symbol. The list of VB2008 arithmetic operators are shown in below:
Table 1: Arithmetic Operators
+ Addition 1+2=3
- Subtraction 4-1=3
^ Exponential 2^4=16
* Multiplication 4*3=12
/ Division 12/4=3
95
Mathematical Operations
96
Visual Programming
2. Exponentiation
3. Multiplication & Division
4. Integer Division
5. Modulus
6. Addition & Subtraction
Comparison Operators
Comparison operators compare two expressions and return a Boolean
value that represents the relationship of their values. Visual Basic
compares numeric values using six numeric comparison operators.
Normally they are used to compare two values to see whether they are
equal or one value is greater or less than the other value. The comparison
will return a true or false result. These operators are shown in Table 2.
< (Less than) Is the value of the first expression 15 < 25 ' True
less than the value of the second?
22 <22 ' False
> (Greater Is the value of the first expression 23 > 33 ' False
than) greater than the value of the
second? 23 > 12 ' True
<= (Less than Is the value of the first expression 23 <= 33 ' True
or equal to) less than or equal to the value of
the second? 23 <= 23 ' True
97
Mathematical Operations
>= (Greater Is the value of the first expression 15 >= 25 ' False
than or equal greater than or equal to the value
to) of the second? 15 >= 15 ' True
Logical Operators
Sometimes we might need to make more than one comparison before a
decision can be made and an action taken. In this case, using numerical
comparison operators alone is not sufficient, we need to use additional
operators, and they are the logical operators. These logical operators are
shown in Table 3.
Table 3: Conditional Operators
Operator Meaning
And Both sides must be true
or One side or other must be
true
Xor One side or other must be
true but not both
Not Negates truth
Unit summary
In this unit you have learned how to recognize and use Visual Basic data.
Visual Basic supports 14 data types, and you must know how to specify
literals and declare variables that take on those data types. Once you
Summary know the data types and variables, you can perform calculations that
assign the results of expressions to variables and controls.
98
Visual Programming
Assessment
Exercise
99