Professional Documents
Culture Documents
Chapter 3
Programming
Fundamentals
Writing Code
By Carlotta Eaton
Objectives...
1. Use the Sub command to create event
procedures and general procedures
2. Use variables and differentiate data types
3. Differentiate between a variable and a
constant
4. Differentiate between Dim and Static
statements
Exploring MS
Objectives
6. Convert an algebraic formula to a Visual
Basic statement; Write a program that
calculates
7. Use the If...Then or Select Case
statements to write code that makes
decisions
8. Write code that repeats using Do...Loop,
For...Next, or For Each ...Next looping
statements
Exploring MS
Module View
Procedure View
Features
Auto
List Members
Auto Quick Info
Auto Syntax Check
Exploring MS
Exploring MS
Exploring MS
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
See next slide for an example
Exploring MS
Exploring MS
10
11
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
Exploring MS
12
Declarations, Variables,
and Constants
Rules for Naming Variables
Exploring MS
13
Declaring Variables
Declaration statement - nonexecutable
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
Exploring MS
14
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
Exploring MS
15
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
Exploring MS
16
Declaring Variables
Data Types
Exploring MS
17
Exploring MS
18
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
Exploring MS
19
20
Exploring MS
21
Functions
Function - unit of code that returns a
value
Build-in Functions
Sqr
- square root
Rnd - random number generator
Int - returns integer portion of a number
Val - converts a string to a value
Exploring MS
22
Functions
Locating built-in functions
Open
Programmer-written functions
Write
Exploring MS
23
Exploring MS
24
WindChill Function
Private Function WindChill( )
Purpose: Calculate the Wind Chill
Reference: National Weather Service
Dim V As Integer Wind Speed Velocity
Dim T AS Integer Temperature
V = hsbSpeed.Value
T = hsbTemperature.Value
WindChill = 0.0817 * (3.71 * Sqr(V) _
+5.81 - (0.25*V)) * (T-91.4)+91.4
End Function
Exploring MS
25
WindChill Function
When the cmdCalculate button is clicked,
the WindChill function is executed
Exploring MS
26
Exploring MS
27
Selection
If...Then...Else statement
Select Case statement
Repetition
Exploring MS
28
Exploring MS
29
Exploring MS
30
Exploring MS
31
32
33
Banner Comments
Add the Load Form Procedure
Add the Checkmark Picture Procedure
Add the Arrow Picture Procedure
Run and Save your Project
Test and Debug your Project
Print your Project Code and Exit
Exploring MS
34
Exploring MS
35
Do While...Loop Structure
Do While condition
statements
Loop
Where
The condition is tested, and if true the loop is
repeated. When the condition is false, the loop
statements are skipped the statement after Loop
is executed.
Exploring MS
36
Do Until...Loop Structure
Do Until condition
statements
Loop
Where
The condition is tested, and if false the loop is
repeated. When the condition is true, the loop
statements are skipped the statement after Loop
is executed.
Exploring MS
37
Do Until...Loop Structure
Calculate the factorial of a number
given by user
Dim Factorial As Double
Dim I As Integer
User gives
number
I = 1
Factorial = 1
Do While I <= Val (txtAnswer.text)
Factorial = Factorial + 1
I = I + 1
Loop
Exploring MS
38
Exploring MS
39
Summary ...
Fundamental programming demonstrated
Variables vs. Constants
Data types
Local vs Global variables
Functions and Procedures
Control structures: sequence, selection and
repetition
Exploring MS
40
Summary
Statements
Dim,
Static, Const
Public, Private
Sub, Function
Do...Loop, Do While...Loop, Do Until...Loop
For Each...Next, For...Next
If...Then...Else
Select Case
Exploring MS
41
42
Case Studies
Calculate a Formula
Weather Calculations
More Fun and Games
More Web Resources for Visual Basic
Exploring MS
43