Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Chapter 3 Programming Fundamentals Writing Code 3 Exploring Microsoft Visual Basic 6.0 Copyright © 1999 Prentice-Hall, Inc. By Carlotta Eaton."— Presentation transcript:

1

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

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

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

5 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

6 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

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

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

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

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

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

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

13 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

14 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)

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

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

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

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

19 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.

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

21 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.20 The Wind Chill Project  Write a program that calculates the wind chill temperature  Find a formula - USA Today Weather Web site www.usatoday.com/weather/wchilform.htm www.usatoday.com/weather/wchilform.htm  Inputs: Wind Speed and Temperature  Outputs: Wind Chill Temperature

22 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.21 The Wind Chill Project  Orginal 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

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

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

25 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

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

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

28 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

29 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

30 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

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

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

33 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

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

35 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.34 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 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.35 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 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.36 ‘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*(Factorial + 1) I = I + 1 Loop Do Until...Loop Structure User gives number

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

39 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.38 Summary Statements Dim, Static, Const Public, Private Sub, Function Do While...Loop, Do Until...Loop For Each...Next, For...Next If...Then...Else Select Case


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

Similar presentations


Ads by Google