Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 3 Programming Fundamentals

Similar presentations


Presentation on theme: "Chapter 3 Programming Fundamentals"— Presentation transcript:

1 Chapter 3 Programming Fundamentals
Exploring Microsoft Visual Basic 6.0 3 Chapter 3 Programming Fundamentals Writing Code Exploring Microsoft Visual Basic 6.0 Copyright © 1999 Prentice-Hall, Inc. By Carlotta Eaton Copyright 1999 Prentice-Hall, Inc.

2 Copyright 1999 Prentice-Hall, Inc.
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 5. Use help to find appropriate predefined function, and then utilize the function Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.

3 Copyright 1999 Prentice-Hall, Inc.
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.

4 Modules and Procedures
Code - the man behind the curtain Modules - large units of code that comprise a Visual Basic application Form Modules - contains a form and code for the form Standard Modules - contains code that is typically used by other modules Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.

5 Modules and Procedures
Procedures - smaller units that comprise a module Event procedure - automatically invoked by an event General procedure - explicitly invoked by another procedure Private procedure - accessible from within a module Public procedure - accessible from anywhere Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.

6 Copyright 1999 Prentice-Hall, Inc.
The Code Editor Window View and edit code Views Full Module View Procedure View Features Auto List Members Auto Quick Info Auto Syntax Check Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.

7 Copyright 1999 Prentice-Hall, Inc.
The Code Editor Window Object List Box Procedure List Box Full Module View button Auto List Members Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.

8 Copyright 1999 Prentice-Hall, Inc.
The Code Editor Window Help window Syntax error in Red Procedure view button Error message box Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.

9 Copyright 1999 Prentice-Hall, Inc.
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 Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.

10 Copyright 1999 Prentice-Hall, Inc.
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 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.

11 Copyright 1999 Prentice-Hall, Inc.
The Sub Statement Example Private Sub cmdCalcTriangle_Click Dim Base As Single Dim Height As Single Area = 1 / 2 * (Base * Height) End Sub Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.

12 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 Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.

13 Declarations, Variables, and Constants
Rules for Naming Variables Must begin with an alphabetic character Can’t 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 (See Appendix A) Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.

14 Copyright 1999 Prentice-Hall, Inc.
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.

15 Copyright 1999 Prentice-Hall, Inc.
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 Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.

16 Copyright 1999 Prentice-Hall, Inc.
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 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.

17 Copyright 1999 Prentice-Hall, Inc.
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 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.

18 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” LoanAmount = 67.38 Length = 17.8 Note: Order is important. Variable name always on the left, and value on the right. Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.

19 Copyright 1999 Prentice-Hall, Inc.
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 Const MaxNumber As Integer = 100 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.

20 Copyright 1999 Prentice-Hall, Inc.
The Wind Chill Project Write a program that calculates the wind chill temperature Find a formula - UST Today Weather Web site Inputs: Wind Speed and Temperature Outputs: Wind Chill Temperature Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.

21 Copyright 1999 Prentice-Hall, Inc.
The Wind Chill Project Orginal formula WC = (3.71(V **0.5) V)(T ) Visual Basic statement WC = * (3.71 * Sqr(V) (0.25 * V)) * (T ) Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.

22 Copyright 1999 Prentice-Hall, Inc.
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.

23 Copyright 1999 Prentice-Hall, Inc.
Functions Locating built-in functions Open the Functions online reference book or search for a function by name Programmer-written functions Write your own functions using the Function statement Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.

24 The Function Statement
Private Function function-name (argument1, argument2....) statements End Function Where Private or Public is required Function indicates the beginning of a function function-name is the name that will be used to call the function ( ) parentheses are required around the argument list argument1, argument2 are optional variables needed to perform the calculation or actions needed for the function End Function indicates the end of the function Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.

25 Copyright 1999 Prentice-Hall, Inc.
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 = * (3.71 * Sqr(V) _ (0.25*V)) * (T-91.4)+91.4 End Function Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.

26 Copyright 1999 Prentice-Hall, Inc.
WindChill Function When the cmdCalculate button is clicked, the WindChill function is executed Private Sub cmdCalculate_Click() WC = WindChill() txtWindChill.Text = Cint(WC) End Sub Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.

27 The Wind Chill Project using Functions and Procedures
Hands-On Exercise 1 Create a New Project Create the Controls for Temperature Input Create the Controls for Wind Speed Input Create the Controls for Wind Chill Output Create the Image Control Add Banner Comments Add the Wind Chill Function Add Temperature and Speed Procedures Save, Run, Test your Project Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.

28 Controlling your Application
Sequence Functions and Procedures Selection If...Then...Else statement Select Case statement Repetition For...Next Loop statement For Each...Next statement Do Loops Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.

29 Learning to Add Project
Specification for the Game purpose of the game is to practice addition skills use numbers between 0 and 9 user is a pre-school age child, in kindergarten, or in first grade user may not yet know how to read game should give visual feedback indicating correct or incorrect answer Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.

30 Copyright 1999 Prentice-Hall, Inc.
Learning to Add Game Reward for correct answer Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.

31 Copyright 1999 Prentice-Hall, Inc.
Learning to Add Game Image for incorrect answer Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.

32 Design the User Interface
Prototype - a partially completed version of an application that demonstrates the “look and feel” of the finished product Usability Testing - end user or client tests design of user interface using a prototype Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.

33 Making Decisions with the Learning to Add Game
Hands-On Exercise 2 Create 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.

34 Making Decisions with the Learning to Add Game
Hands-On Exercise 2 (continued) Add 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.

35 For...Next Loop Structure
For counter = start To end Step increment statements Next counter Where Counter is tested to see if less than end. If so, repeat loop again. If not, go to statement after Next. Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.

36 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 Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.

37 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 Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.

38 Do Until...Loop Structure
‘Calculate the factorial of a number given by user Dim Factorial As Double Dim I As Integer I = 1 Factorial = 1 Do While I <= Val (txtAnswer.text) Factorial = Factorial + 1 I = I + 1 Loop User gives number Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.

39 Loops with the Marching Band Project
Hands-On Exercise 3 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 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.

40 Copyright 1999 Prentice-Hall, Inc.
Summary ... Fundamental programming demonstrated Variables vs. Constants Data types Local vs Global variables Functions and Procedures Control structures: sequence, selection and repetition Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.

41 Copyright 1999 Prentice-Hall, Inc.
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.

42 Practice with Visual Basic
1. Learning to Subtract Game 2. Wind Chill in Celsius 3. Heat Index 4. Rock, Paper, Scissors Game 5. Creating Variables 6. Event-driven Programming 7. For...Next Loop 8. Digital Clock Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.

43 Copyright 1999 Prentice-Hall, Inc.
Case Studies Calculate a Formula Weather Calculations More Fun and Games More Web Resources for Visual Basic Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.


Download ppt "Chapter 3 Programming Fundamentals"

Similar presentations


Ads by Google