When we write a lengthy program some times we may have to write a set of statements repeatedly. It will be a time consuming one. So to avoid that we can write the code and deep in separate file and when ever we need to execute the statements we can call it. The advantages of using procedures in programming are (a) It is easier to debug a program with a procedures, which breaks a program into discrete logical limits. (b) Procedures used in one program can act as building blocks for other programs with slight modifications.
General Procedures
A general procedure tells the application how to perform a specific task. Once a general procedure is defined, it must be specifically invoked by the application. By contrast, an event procedure remains idle unit called upon to respond to events caused by the user or triggered by the system.
Why create general procedures? One reason is that several different event procedures might need the same actions performed. A good programming strategy is to put common statements in a separate procedure (a general procedure) and have your event procedures call it. This eliminates the need to duplicate code and also makes the application easier to maintain. For example, the VCR sample application uses a general procedure called by the click events for several different scroll buttons. Figure2.3 illustrates the use of a general procedure. Code in the click events calls the button manager sub procedure, which runs its own code, and then returns control to the click event procedure.
Return
Return
Return
Event Procedures
General Procedure
With common code
Figure: General procedures are called by event procedures
Event procedures
When an object in Visual Basic recognizes that an event has occurred, it automatically invokes the event procedure using the name corresponding to the event. Because the name establishes an association between the object and the code, event procedures are said to be attached to forms and controls.
- An event procedure for a control combines the control’s actual name (specified in the Name property), an underscore (_) and the event name. for instance, if you want a command button named cmd play to invoke an event procedure when it is clicked, use the procedure cmdplay – click
- An event procedure for a from combines the word” Form”, an underscore, and the event name. If you want a form to invoke an event procedure when it is clicked, use the procedure From _ click. (Like controls, forms do have unique name, but they are not used in the names of event procedures). If you are using the MDI form, the event procedure combines the word “MDI form”, an underscore, and the event name, as in MDIF orm_Load.
All event procedures use the same general syntax.
Syntax for a control event
Syntax for a from event
Private Sub controlname _ eventname (arguments)
Statements
End sub
Private Sub form_ eventname (arguments)
Statements
End Sub.
Although you can write event procedures from scratch, it’s easier to use the code procedures provide by Visual Basic, which automatically include the correct procedure names. You can select a template in the Code Editor window by selecting an object from the object box and then selecting a procedure from the procedure box.
It’s also a good idea to set the name property of your controls before you start writing event procedures for them. If you change the name of a control after attaching procedure to it, you must also change the name of the procedure to match the new name of the control. Otherwise, Visual Basic won’t be able to match the control to procedure. When a procedure name does not match a control name, it becomes a general procedure.
Creating And Calling Function
You have now met two types of sub procedures. The firs type (Private by default) is the event dub procedure behind forms and controls. These event procedures. Are already defined for you.The second type the ones you add yourself are called general sub procedures. There are also functions procedures.These you must create yourself, and they can be private of public. Function procedures return a value to the procedures that call them. The best way to understand function procedures is to try one out. Here’s one that calculates the cube root of a number.
- Open a form ‘ code window by double –clicking it in the For Designer
- Crete the template by typing the following on a blank line in a form’s code window and pressing enter.
Public Function Cube Root
This Creates the following Coe:
Public Function Cube Root ()
End Function
- Now alter the template as follows
Public function Cube root (x as Double) as Double
If x=0 then
Cube Root =0
Exit function
End if
Cube Root = 10 ((Log (abs(x))/ Log 910)/3)
If x <0 then
Cube Root = Cube Root
End if
End function
The math involved here isn’t important. But a few other things are:
- The function ends with an End Function rather than an End Sub
- You can jump out of a function with exit function.
- The function must return a value the line Cube Root = 0 is one of the three possible lines that do that.
- You can specify the typ0e of value returned here it’s a Double (a numeric variable that can handle very large and small values as well as decimals) by adding the statements as followed by the data type of the return value.
- To call this function, you must assign its return value to another variable. Try the following code in a click event for a form.
Dim Y As Double
Y=Cube Root (27)
Print Y
- Now run the application and click the command button.
This code prints the return value straight onto the form
Normally, you may want to assign the return value to a control on the form, such as a text box control.
txtText1.Text = Y
the result is
Alternatively, you can have just the line
TxtText1.Text=CubeRoot (27)
Here the return value is being assigned directly to a control.
Hence you call a function procedure, you must enclose the parameter (s)in parentheses. This contrast with calling a sub procedure. |
Design – Time, Run – Time, And Break Modes
Understanding the three modes of operations of the Visual Basic will ease the debugging process. Applications are built in Design-time mode and run in run time mode. This chapter introduces break mode, which suspends the execution of the program so that data can be examined and alerted.
Identifying the current mode
The Visual Basic title bar always shows the current mode. The following Figures show the title bar for design time, run time, and break mode.
While an application is running, a situation may occur to switch to the suspend mode (break mode). For example, the execution of the application may be imprisoned in an indefinites loop. In this case the running application mighty be switched to the creak mode this is done:
- By choosing the break from Run menu.
- By clicking the break button.
- By pressing the CTRL +BREAK.
In the suspend mode, it is possible to view and edit code (choosing code from the view menu. Or pressing F7), example or modify data, restart the application, end execution, or continue execution from the same point.
Using the tool bar to change modes
The toolbar provides three buttons that lets to change quickly from one mode to another. These buttons appear as in Figure 7.2
Whether any of these buttons is available depends on whether Visual Basic is in run time mode, design time mode, or break mode. The following table lists the buttons available for different modes.
Mode | Toolbar button available |
Design time | Start |
Run Time | Break, End |
Break | Continue, End (in break mode, the start button becomes thecontinue button) |