using general sub procedures

25
Using General Sub Procedures/Routine s and functions in Applications

Upload: danica-denice-epino

Post on 22-May-2015

398 views

Category:

Technology


2 download

DESCRIPTION

I hope this can help

TRANSCRIPT

Page 1: Using general sub procedures

Using General Sub Procedures/Routines

and functions in Applications

Page 2: Using general sub procedures

• Using General Sub procedures can help divide a complex application into more manageable units of code. This helps meet the above stated goals of readability and reusability.

• Most applications have tasks not related to objects that require some code to perform these tasks. Such tasks are usually coded in a general sub procedure, essentially the same as a routine in other languages.

Page 3: Using general sub procedures

• Breaking your programs into as many small but logical sections as possible. the smaller routines make your programming and subsequent maintenance easier.

• In many traditional programming languages such as COBOL and FORTRAW, a program is like a long book without chapters. The code goes on and on and the program’s length is exceeded only by the boredom programmer’s face trying to wade through the code hunting down errors.

•  

Page 4: Using general sub procedures

A Visual Basic program consists of:

• A form with controls that act as the program’s background and user interface

• A general-purpose procedure named, found in the Code Window’s object dropdown list

• Event procedures that tie the controls together and add specific direction and calculations to the application

• A CONSTANT.TXT file that provides named constants used by your code

Page 5: Using general sub procedures

Kinds of Procedures

Event-procedures-executes as events occur

Procedure is really not an executable procedure in the way that event procedures execute

Page 6: Using general sub procedures

Defining a Sub Procedure/Routines

• A sub procedure or a subroutine is a procedure that executes the lines of code within its block but doesn’t return a value. The syntax for a simple sub is as follows:Sub GenlSubProc (Arguments)Definition Header

:End Sub

Page 7: Using general sub procedures

The definition header names the Sub procedure and defines any argument passed to the procedure.

Arguments are a comma-delimited list of variables passed to and/or from the procedure.

If there are arguments, they must be declared and typed in the definition header in this form: Var1as Type1, Var2 as Type2…

A subroutine always begin with the Sub statement and always ends with the End ?Sub statement.

A subroutine may or may not be an event procedure

Page 8: Using general sub procedures

*Example of a code that contains an event-procedure

• Event procedures are specific subroutines tied directly to control events.

• When you write a section of code that your application will have to execute more than once, that section of code is a great candidate for a general-purpose, non-event subroutine.

Sub cmdExit_Click ()EndEnd Sub

Page 9: Using general sub procedures

BENEFITS FROM USING SUB PROCEDURE ROUTINES

Eliminates redundant codes

Codes will be reusableProgram code will be simpler to maintain

Page 10: Using general sub procedures

MAKING SUBROUTINE

• You can add a sub to your project in two ways:

• By writing the code directly into the General Declarations section of a form or module; or

• By using the Tools menu’s Add Procedure option

Page 11: Using general sub procedures

ENABLING THE ADD PROCEDURE MENU ITEM

For the Add procedure menu item to be enabled, you must be in the Code window view of the form or module into which you want to add the procedure.

ADD A SUB TO YOUR PROJECT WITH THE ADD PROCEDURE

1. From the Tools menu, choose Add procedure to display the Add procedure dialog.

2. Enter the Sub name.3. Click OK to add the sub’s code block to the

form or module.

Page 12: Using general sub procedures

TYPING THE SUBROUTINE IN THE CODE WINDOW

1.Go to the Code Window.2.Type Sub GenAverage.3.Press Enter key.

Page 13: Using general sub procedures

In selecting the Insert Procedure Menu item, note another option for your procedure is Scope. You have the option of Public or Private.

If a module procedure is public, it can be called from any other procedure in other module.

If a module procedure is Private, it can only be called from the module it is defined in.

NOTE: Scope only applies to procedures in modules.

*All event procedures and general procedures in a form are Private.

Page 14: Using general sub procedures

CALLING A SUB PROCEDURE

• Method 1: Call GenlSubProc (Arguments) (if there

are no Arguments, do not type the parentheses)

• Method 2: GenlSubProc Arguments

CREATING A PROCEDURE1. Create a project.2. Add the following controls in your form

with the corresponding properties

Page 15: Using general sub procedures

Object Properties ValuesLabel 1 Caption US DollarLabel 2 Caption Peso

Exchange Rate

Label 3 Caption Peso ValueText 1Text 1Text 2Command 1

Name cmdConvert

Caption &Convert

Page 16: Using general sub procedures

3. Add a procedure name USPesoConvert by using the Add Procedure in the tools menu.

4. Type the following code for each procedure as shown below: 

Private Sub cmdConvert_Click ()Call UsPesoConvert

End Sub

Page 17: Using general sub procedures

Private Sub Form_Load()Text 1 = “ “Text 2 = “ “Text 3 = “ “

End Sub

Public Sub USPesoConvert ()Text 3 = Text1 & Text2

End Sub

Page 18: Using general sub procedures

To call dollar exchange routine, we could use:Private Sub cmdConvert_Click ()

Call USPesoConvertEnd Sub

Or Private Sub cmdConvert_Click ()

USPesoConvertEnd Sub 5. Press F5 to execute the program. 

Page 19: Using general sub procedures

PASSING ARGUMENTS TO SUB PROCEDURES

You can enhance the power and versatility of subs and functions by using arguments.

ARGUMENTS – also referred to as a parameter

PARAMETER- a variable that acts as a placeholder for a value that you’ll pass into the sub or functiobn.

Using arguments greatly increases the reusability of your code.

Page 20: Using general sub procedures

THE ADVANTAGES OF THE LATTER METHOD IS TWOFOLD:

One call satisfies many needs throughout your code.

If you need to enhance this functionality, you don’t have to go through your code and make enhancements line by line, you simply go back to the function and make the changes within the function’s code block.

Page 21: Using general sub procedures

USING GENERAL FUNCTION PROCEDURES IN APPLICATIONS

Function procedures work a lot like subroutine; you can call them from elsewhere in the program. Unlike subroutine procedures, however, function procedures return values.

Page 22: Using general sub procedures

In Syntax

• Private/Public are the optional Visual Basic keywords that define the scope of the function.

• Function is the Visual Basic keyword that denotes the procedure is a function/

• FunctionName is the name that you assign to your function.

• As is the Visual Basic keyword that denotes the procedure is a function.

• DataType is the data type of the value that the function will return.

Page 23: Using general sub procedures

• ReturnValue is the value that you pass back from the function by assigning it top the function’s name (this is very important).

• End Function is the Visual Basic keywords that denote the end of a code block.

• You add a function to your project by using the same two methods that you use to add a subroutine. However, he advised that you have to manually add a little code when you add a function to your code by using the Add Procedure dialog.

Page 24: Using general sub procedures

• A function procedure always begins with the Function statement and ends with the End function statement.

• A function is the same in every way as a subroutine except that the function returns a value.

• All you do to call a function is to use the function procedure’s name inside an expression or statement.

• If you ever need to exit a function procedure before the function’s normal termination, use the Exit function statement.

Page 25: Using general sub procedures

LOCATING FUNCTION PROCEDURES

• Can be located in forms or modules.• They are created using

exactly the same process described for Sub procedures. The only difference is you use the keyword FUNCTION.