Anda di halaman 1dari 41

Topics:

Variable, Data type

Data Type.!
Data type

Storage
size

Range

Byte

1 byte

0 to 255

Boolean

2 bytes

True or False

Integer

2 bytes

-32,768 to 32,767

Long (long integer)

4 bytes

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

Single (single-precision 4 bytes


floating-point)

-3.402823E38 to -1.401298E-45 for negative


values; 1.401298E-45 to 3.402823E38 for positive
values

Double (double8 bytes


precision floating-point)

-1.79769313486232E308 to -4.94065645841247E324 for negative values; 4.94065645841247E-324


to 1.79769313486232E308 for positive values

Currency (scaled
integer)

-922,337,203,685,477.5808 to
922,337,203,685,477.5807

8 bytes

Data Type.!
Data type

Storage size

Range

Date

8 bytes

January 1, 100 to December 31,


9999

Object

4 bytes

Any Object reference

String (variable-length)

10 bytes + string length

0 to approximately 2 billion

String (fixed-length)

Length of string

1 to approximately 65,400

Variant (with numbers)

16 bytes

Any numeric value up to the range


of a Double

Variant (with characters)

22 bytes + string length

Same range as for variable-length


String

User-defined (using Type)

Number required by
elements

The range of each element is the


same as the range of its data type.

Data Type.!

In all probability, in 90% of our applications


we will use at most six types: String, Integer,
Long, Single, Boolean and Date. The
Variant type is often used automatically when
type is not important. A Variant-type field
can contain text or numbers, depending on
the data that is actually entered. It is flexible
but it is not very efficient in terms of storage.

Variables

Variables

Simply computer memory locations that can store


specific types of data and are given special names

Selecting a data type for a variable

Data type is the kind of data a variable can store


Integer data types: Integer, Short, and Long
Floating-point data types: Single and Double
Other special data types: Boolean, Byte, Char,
Decimal, and String

Variable Names.

Variable Names
The following are the rules when naming the variables in Visual
Basic
------ It must be less than 255 characters
------No spacing is allowed
------It must not begin with a number
-------Period is not permitted
Examples of valid and invalid variable names are displayed
below:
Valid Name
Invalid Name
My_Car
My.Car
this year
1NewBoy
Long_Name_Can_beUSE
He&HisFather
*& is not acceptable

Assigning Values to
Variables.

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 etc. The
following are some examples:
firstNumber=100
secondNumber=firstNumber-99
userName="John Lyan"
userpass.Text = password

Assigning Values to
Variables.
Label1.Visible = True
Command1.Visible = false
Label4.Caption = textbox1.Text
ThirdNumber = Val(usernum1.Text)
total = firstNumber + secondNumber
+ThirdNumber

Assigning Data to an
Existing Variable

Assignment statement

A variable may be assigned a literal constant

variablename = value
Value does not change while an application is running

Variables must be assigned values of the correct


data type
Literal type character

Can change a numeric literal constants data type to


match that of a variable

Declaring Variable....

Declaring a variable means giving it a name,


a data type and sometimes an initial value.
The declaration can be explicit or implicit. .
An explicit declaration: variable is declared
in the Declarations Section or at the
beginning of a Procedure. An explicit
declaration looks like:
Dim MyNumber As Integer.
Now the variable MyNumber exists and a 2byte space has been reserved for it.

Declaring Variable....

An implicit declaration: the variable is declared "on


the fly", its data type is deduced from other
variables. For example:
Dim Total1 As Integer
'Explicit declaration
Dim Total2 As Integer
'Explicit declaration
Total3 = Total1 + Total2
'Implicit declaration
Total3 is not formally declared but is implied, it is
"arrived at" from the other declarations.

Declaring Variable....

Other examples of declarations:


Dim MyName As String
Dim StudentDOB As Date
Dim Amount5, Amount6, Amount7
In the last example the type assigned to each
variable will be: Variant. It is the default type when
none is specified.
There can be multiple explicit declarations in a
statement:
Dim EmpName As String, SalaryMonth As
Currency, SalaryYear As Currency

Declaring Variable....

In this final example, what are the types assigned to the three
variables:
Dim Amount1, Amount2,
Amount3 As Single
All Single-precision floating point, But only Amount3 is Single.
Amount1 and Amount2 are considered Variant because VB
specifies that each variable in a statement must be explicitly
declared. Thus Amount1 and Amount2 take the default data type.

Named Constants

A constant is a value that does not change during the


execution of a procedure. The constant is defined with:
Const Constant Name As Data Type = Value
Const Pi As Single=3.142
Const Temp As Single=37
Const Score As Single=100

Named constant

Like a variable, is a named memory location


The value of a named constant cannot be changed while
the application is running
Useful for holding constants that are used repeatedly
throughout an application

Or that may need to be changed by the programmer over a


period of time

Named Constants
(continued)

Mathematical and Text


operators.
Operator

Definition

Example

Result

Exponent (power of)

4^2

16

Multiply

5*4

20

Divide

20 / 4

Add

3+4

Subtract

7-3

Mod

Remainder of
division

20 Mod 6

Integer division

20 \ 6

&

String concatenation

"Joan" & " " &


"Smith"

"Joan Smith"

Logical operators.
Operator

Definition

Example

Result

Equal to

9 = 11

False

>

Greater than

11 > 9

True

<

Less than

11 < 9

False

>=

Greater or equal

15 >= 15

True

<=

Less or equal

9 <= 15

True

<>

Not equal

9 <> 9

False

AND

Logical AND

(9 = 9) AND (7 False
= 6)

OR

Logical OR

(9 = 9) OR (7
= 6)

True

Suffixes..

Suffixes for Literals


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. Some of the suffixes are
displayed here.
Suffix
Data Type
&
Long
!
Single
#
Double
@
Currency

Suffixes..

In additon, 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#

Logical Operators..

Logical Operators
In addition to conditional operators, there are a few
logical operators which offer added power to the VB
programs. There are shown below
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

Thank You

Making Decisions in
Programs

Ability of an application to make decisions

Vital part of program functionality

The Selection Structure

Three basic structures used in programming

Sequence structure
Selection structure
Repetition structure

Selection structure

Different sets of instructions are processed


depending on certain conditions in the program

Coding the If and If/Else


Selection Structures

The MessageBox.Show
Method

Syntax

MessageBox.Show(text, [caption,
buttons, icon[,defaultButton])

Displays a dialog box that contains a text

The If/ElseIf/Else
Structure

Facilitates decisions with more than two


choices
Equivalent to multiple nested If/Else
structures

The Case Selection


Structure

The Case Selection


Structure (continued)

Repeating Program
Instructions

Objectives

Review how to perform looping


Work with several different kinds of Visual Basic
controls related to the repetition structure

The Repetition Structure

Repetition structure (also called a loop)

Repeats a block of code (called the loop body)


while a certain condition is true

Types of repetition structures

Pretest loop

Evaluates condition before loop body executes

Posttest loop

Evaluates condition after loop body executes once

The Repetition Structure


(continued)

Repetition statements

For...Next
Do...Loop
For Each...Next

The ForNext Statement

The DoLoop Statement

Used to code both a pretest loop and a


posttest loop

The DoLoop Statement


(continued)

The DoLoop
Statement(continued)

The InputBox Function

Function

Predefined procedure that performs a specific


task

And then returns a value after completing the task

InputBox function

Displays a predefined dialog box that contains a


message

Along with an OK button, a Cancel button, and an input


area in which the user can enter information

The InputBox Function


(continued)

The InputBox Function


(continued)

The Sales Express


Application

Thank You

Anda mungkin juga menyukai