Beruflich Dokumente
Kultur Dokumente
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
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 Visual Basic 6 Copyright 1999 Prentice-Hall, Inc. 3
Features
Auto
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
12
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 Visual Basic 6 Copyright 1999 Prentice-Hall, Inc. 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
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
16
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
17
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
19
www.usatoday.com/weather/wchilform.htm Inputs: Wind Speed and Temperature Outputs: Wind Chill Temperature
20
= 0.0817(3.71(V **0.5) + 5.81 0.25V)(T - 91.4) + 91.4 = 0.0817 * (3.71 * Sqr(V) + 5.81 (0.25 * V)) * (T - 91.4) + 91.4
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 Visual Basic 6 Copyright 1999 Prentice-Hall, Inc. 22
Functions
Locating built-in functions
Open
the Functions online reference book or search for a function by name your own functions using the Function statement
Programmer-written functions
Write
23
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 Visual Basic 6 Copyright 1999 Prentice-Hall, Inc. 25
WindChill Function
When the cmdCalculate button is clicked, the WindChill function is executed
26
Selection
Repetition
For...Next Loop statement For Each...Next statement Do Loops
30
31
the Learning to Add Project Add Controls to Display the Problem Set Common Properties Set Individual Control Properties Add More Images and Set Properties Add Picture Box Controls for Icon Buttons
Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc. 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 Visual Basic 6 Copyright 1999 Prentice-Hall, Inc. 34
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.
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.
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 Visual Basic 6 Copyright 1999 Prentice-Hall, Inc. 38
Create the Marching Band Project Add Command Buttons and a Timer Add Controls to Display the Band Add Banner Comments Add Code for Buttons and the Form Add Code to Add Animation Run, Save, Test and Debug Your Project Create a Copy of the Project Change Animation Code Update, Run, Save, Test and Debug Exit Visual Basic
Copyright 1999 Prentice-Hall, Inc. 39
Summary ...
Fundamental programming demonstrated Variables vs. Constants Data types Local vs Global variables Functions and Procedures Control structures: sequence, selection and repetition
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 Visual Basic 6 Copyright 1999 Prentice-Hall, Inc. 41
Case Studies
Calculate a Formula Weather Calculations More Fun and Games More Web Resources for Visual Basic
43