Com 211

Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

By

Okimba P.E.
 During this week you will learn:
◦ Basic character sets
◦ Data Types
Visual Basic uses building blocks such as
Variables, Data Types, Procedures, Functions
and Control Structures in its programming
environment.
Modules
Code in Visual Basic (VB) is stored in the form of
modules. There are three kinds of modules in
VB;
 Form Modules,
 Standard Modules and
 Class Modules.
Form -the container for all the controls that make up the user interface.
Standard Module - separate module containing a procedure
created to implements common code.
Class module -(CLS filename extension) are the foundation of the
object oriented programming in Visual Basic.
Note:
A Module contains Declarations and Procedures
Character set refers to those characters acceptable or
allowed in VB programming e.g. GB is unknown in
English likewise X is unknown in Yoruba, Igbo and
Hausa languages.
The Microsoft character sets consist of the following:
 Alphabet A/ a – Z/z (Both Upper and Lower Case)
 Numeric Digits 0 – 9
 Decimal point (.)
 Grouping Characters (e.g. Comma, colons,
Semicolons, single and double apostrophe,
parenthesis)
 Relational Operators (eg. =, <>,>,< etc) Arithmetic
Operators (eg. +, -, /, =. etc) Blank character
The VB programming language works with all kinds
of data. Before you learn how to manipulate data,
you will learn how to distinguish among the various
data types that supports the language. Some data
falls into more than one category. For example, a
dollar amount can be considered both as a currency
data type and as a single data type. When you write
a program, you need to decide which data type best
fits your program's data values. Thus, we are
considering data types in two categories;
 Numeric data types
 Non-numeric data types
Literals are values that you assign to a data.
In some cases, we need to add a suffix behind a
literal so that VB can handle the calculation
more accurately. For example, we can use
num=1.3089# for a Double type data.
Examples of other suffix used in VB are; !, @
and &
In this Lesson you will learn:
◦ Declaring Variables
◦ Assigning Values to Variables
◦ Mathematical Expressions
◦ Conditional Operators
 Definition: To define a variable means to
create and name a variable.
A variable is a letter or name that can store a
value.
When you create computer programs, you can
use variables to store numbers, such as the
height of a building, or words, such as a
person's name. Simply put, you can use
variables to represent any kind of information
your program needs.
Storing Information in Variables
There are three steps to using a variable:
 Declare the variable. Tell the program the
name and kind of variable you want to use.
 Assign the variable. Give the variable a value
to hold.
 Use the variable. Retrieve the value held in
the variable
Declaring Variables
A program can have as many variables as you
need it to have. When you declare a variable,
you have to decide what to name it and
what data type to assign to it (That is a variable
must have; name and data type).
Example.
totalCandy, W123, name21478 etc.
Dim statement.
Dim - Dimension
This statement is used for declaring variables in VB
programming.
i.e. Dim VariableName As DataType
Note:
Using Dim, you are telling the interpreter
◦ that you need a variable
◦ what to name the variable
◦ what kind of variable you want
Format of Dim Statement:
Dim VarName AS DataType
Rules for Variable names formation.
 A name must begin with a letter and it can be either uppercase
or lowercase letters.
 After the initial letter, names can contain letters, numbers, or
underscores in names.
 May be as much as 255 characters long (but don't forget that
somebody has to type the stuff!).
 Must not contain a space or an embedded period or type-
declaration characters used to specify a data type; these are ! # %
$&@
 Must not be a reserved word (that is part of the code, like
Option, for example)
 The dash, although legal, should be avoided because it may be
confused with the minus sign. Instead of First-name use
First_name or FirstName.
 Never define two variables with the same name
Examples of variable names are;

Valid Name Invalid Name


My_Car My.Car

this year 1NewBoy

He&HisFather *& Not allowed


Long_Name_Can_Be_USED
The following statements define integer, single-
precision, and double-precision variables:
Dim Length As Integer
Dim Price As Single
Dim Structure As Double
Dim yourName As String
You may also combine them in one line,
separating each variable with a comma, as
follows:
Dim Length As Integer, Price As Single, Structure
As Double, yourName As String, ....
If you want to write a program that stores the user's
text box entry for the first name, you would define
a string like this:
Dim FirstName As String
You can get fancy when you define strings. This
FirstName string can hold any string from 0 to
65,500 characters long. You will learn in the next
section how to store data in a string. FirstName can
hold data of virtually any size. You could store a
small string in FirstName— such as "Joe"—and then
store a longer string in FirstName—such as
"Mercedes". FirstName is a variable-length string.
Initializing or Assigning Values to VB Variables
Initialization or assigning values is performed after
declaring various variables in the Dim statement
using the Visual Basic assignment operator (=). To
initialize a single variable when it is declared:
Dim firstNum As Integer = 10
 When declaring multiple variables each variable
may be initialized in the declaration line:
Dim strCustomerName As String = ―Travis‖, intInterestRate = 10
As Integer
Assigning new values to VB variables
Once a variable has been declared, a new value can be assigned to
the variable at any time using the variable name and the assignment
(=) operator. In the following sample Visual Basic code, a variable is
declared and initialized to 10. It is then re-assigned the value of 30:
intInterestRate = 10 As Integer
intInterestRate = 30
Referencing variable values
Accessing the value of a variable in Visual Basic code is as simple as
typing the variable name. For example, the following code example
adds 20 to the value of intInterestRate and assigns the result to
the intNewRate variable:
Dim intInterestRate As Integer = 5
Dim intNewRatae As Integer = 0
intNewRate = intInterestRate + 30
Definition: A null string is a zero-length empty
string that is often represented as "".
When you first define variables, stores zeroes in
the numeric variables and null strings in the string
variables. Use the assignment statement when you
want to put other data values into variables.
Variables hold data of specific data types, and many
lines inside a program's Code window consist of
assignment statements that assign data to
variables. Here is the format of the assignment
statement:
VarName = Expression
Suppose that you defined Title to be a string
variable with a fixed length of 10, but a user types
Mondays Always Feel Blue in a text box's Text
property that you want to assign to Title. It stores
only the first ten characters of the control to Title
and truncates the rest of the title. Therefore, Title
holds only the string "Mondays Al".
This is the first of several program code reviews
that you will find throughout the rest of the
lectures.
Sub cmdJoke_Click ()
cmdJoke.Caption = "Bird Dogs Fly"
End Sub
Example
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim password As String
Dim yourName As String
Dim firstnum As Integer
Dim secondnum As Integer
Dim total As Integer
Dim doDate As Date
End Sub
Values are assigned in the above program in the following way;
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
Constants are different from variables in the
sense that their values do not change during
the running of the program.
Declaring a Constant
The syntax to declare a constant is
Const Constant Name As Data Type = Value
Example
Private Sub Form1_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.LoadConst
Pi As Single=3.142Const Temp As Single=37Const Score
As Single=100
End Sub
Read the following;
 Block Level Scope
 Procedure Level Scope
 Module Level Scope
 Global Scope
 Static Variable in VB
 Visual Basic operators
Next class will be on
VB controls

You might also like