Lecture 8
Visual Basic (2)
CC111 Lec8 : Visual Basic
Objectives
The student should learn more about writing more
advanced Visual Basic code.
The student should learn the precedence rules for writing
equations.
CC111 Lec8 : Visual Basic
Syntax Boxes
Syntax for a Visual Basic statement is shown in a syntax
box
Reserved words are shown in bold
Programmer named words are shown in italics
CC111 Lec8 : Visual Basic
The Sub Statement
Private Sub controlname_eventname( )
Statements
End Sub
Where
Private is the default procedure type
Sub indicates beginning of procedure
controlname is name of associated control
_ (underscore) required separator
eventname is name of corresponding event
( ) set of parentheses is required
End Sub indicates end of a procedure
CC111 Lec8 : Visual Basic
The Sub Statement
Example
Private Sub cmdCalcTriangle_Click
Dim Base As Single
Dim Height As Single
Dim Area As Single
Area = (1 / 2) * (Base * Height)
End Sub
CC111 Lec8 : Visual Basic
Declarations, Variables, and Constants
Variable - a uniquely named storage location that contains
data that changes during program execution
Constant - a uniquely named storage locations that
contains data that does not change during program
execution
CC111 Lec8 : Visual Basic
Declarations, Variables, and Constants
Rules for Naming Variables
Must begin with an alphabetic character
Cant contain a period or type-declaration
characters such as
%, &, !, #, @ or $
Must be unique with same scope
Must be no longer than 255 characters
Should not reserved word
CC111 Lec8 : Visual Basic
Declaring Variables
Declaration statement non executable code that sets aside
storage locations for future use
Local variables - declared within a procedure or function
Global variables - declared in the general section of the
application
CC111 Lec8 : Visual Basic
Declaring Variables
Declare variables using the Dim or Static statements
Dim statement - value of variable
preserved only until procedure ends
Static statement - value of variable
preserved the entire time the application
is running
CC111 Lec8 : Visual Basic
The Dim Statement
Dim variablename As datatype
Where
Dim is required
variablename should be a descriptive name
As is required
datatype is one of the following types:
Boolean, Byte, Date, Integer, Long,
Single, Double, Currency, String, Object or Variant
CC111 Lec8 : Visual Basic
Declaring Variables
Data Types
Boolean - True or false
Date - From Jan 1, 100 to Dec 31, 9999
Integer - Numbers without a decimal point
Long - Long integer
Single - Numbers with a decimal point
Double - Long Single
Currency - Dollar amounts
String - Character and alphanumeric data
Object - Any object reference such as Word document
Variant - default, can hold any data type
CC111 Lec8 : Visual Basic
Assigning Values to Variables
Variablename = value
Where
variablename is the descriptive name of the variable
= is the assignment operator
value is the value the variable will contain
Examples:
Number1 = 5
FirstName = Steve
Length = 17.8
Note: Order is important. Variable name always on the left, and
value on the right.
CC111 Lec8 : Visual Basic
Declaring Constants
Const constantname As datatype = value
Where
Const is required
constantname is the descriptive name of the constant
As is required
datatype is the type of data the constant will contain
= is the assignment operator
value is the value of the constant
Examples:
Const Pi As Single = 3.14159265358979
Const MaxNumber As Integer = 100
CC111 Lec8 : Visual Basic
Functions
Function - unit of code that returns a value
Built-in Functions
Sqr - square root
Rnd - random number generator
Int - returns integer portion of a number
Val - converts a string to a value
Str - converts a value to a string
CC111 Lec8 : Visual Basic
Application 1: Calculate the area of a circle
Inputs: radius of the circle r
Output: area of the circle
Process: Area=
CC111 Lec8 : Visual Basic
Application 1: Calculate the area of a
circle (Form View)
CC111 Lec8 : Visual Basic
Application 1: Calculate the area
of a circle (Code View)
CC111 Lec8 : Visual Basic
Application 1: Calculate the area
of a circle (Run)
CC111 Lec8 : Visual Basic
Precedence Table
Operator
()
^
*, /
+,-
If precedence of two following operators is
equal, then the evaluation starts from left to
right.
CC111 Lec8 : Visual Basic
Simple Examples of Equations
Equation in normal
form:
1.
2.
3.
4.
Y=3X
Y=X-10+3(X-Z)
Y=X^2*10
Y=
X
Equation in VB
form:
1. Y=3*X
2. Y=X-10+3*(XZ)
3. Y=X*X*10
4. Y=SQR(X)
CC111 Lec8 : Visual Basic
Application 2: Convert from Fahrenheit Degree to Celsius
Original formula: Celsius=5/9 (Fahrenheit - 32)
Visual Basic formula: Celsius=5/9*(Fahrenheit-32)
Input: Fahrenheit
Output: Celsius
Process: Celsius=5/9*(Fahrenheit-32)
CC111 Lec8 : Visual Basic
Application 2
Convert from Fahrenheit Degree to Celsius (Form View)
CC111 Lec8 : Visual Basic
Application 2
Convert from Fahrenheit Degree to Celsius (Code View)
CC111 Lec8 : Visual Basic
Application 2
Convert from Fahrenheit Degree to Celsius (Run)
CC111 Lec8 : Visual Basic
Application 3
The Wind Chill Application
Write a program that calculates the
wind chill temperature
Inputs: Wind Speed and Temperature
Outputs: Wind Chill Temperature
CC111 Lec8 : Visual Basic
Application 3
The Wind Chill Application
Original formula
WC = 0.0817(3.71(V **0.5) + 5.81 - 0.25V)(T - 91.4) +
91.4
Visual Basic statement
WC = 0.0817 * (3.71 * Sqr(V) + 5.81 -(0.25 * V)) * (T 91.4) + 91.4
Output: Wind Chill (WC)
Input 1 T: Temperature
Input 2 V: Wind Speed
CC111 Lec8 : Visual Basic
Adding Controls and Changing their Properties
CC111 Lec8 : Visual Basic
Writing the Code
CC111 Lec8 : Visual Basic
Running the Application
CC111 Lec8 : Visual Basic