Procedure |
|
Sub Procedures |
|
A Sub procedure is a block of code that is executed in response to an event. |
By breaking the code in a module into Sub procedures, it becomes much easier to find or modify the code in your application. |
|
Syntax: |
|
[Private|Public][Static]Sub procedurename (arguments) |
statements |
End Sub |
|
Each time when procedure is called, the statements between Sub and End Sub are executed. Sub procedures can be placed in standard modules, class modules, and form modules. |
Sub procedures are by default Public in all modules, that means they can be called from anywhere in application. |
|
Note that: |
|
Sub Procedures are the procedures that do not return a value. |
The arguments for a procedure are like a variable declaration. |
|
Event Procedures |
|
When an object in Visual Basic recognizes that an event has occurred then it automatically invokes event procedure |
That event procedure will use procedure name corresponding to that event. |
|
Syntax: |
|
Private Sub Form_eventname (arguments) |
statements |
End Sub |
CmdSave_Click():-Here Click is an event and CmdSave_Click() is the procedure associated with it. |
|
Syntax for a Control Event: |
|
Private Sub Control_Eventname(arguments) |
Statements Block |
End sub |
|
Syntax for a Form Event: |
|
Private Sub Form_eventname(arguments) |
Statements Block |
End sub |
|
Example: |
|
Private Sub Form_Load(). |
This is the main event procedure for a form .When a form is loaded in application then this procedure is called first. |
|
General Procedures |
|
A general procedure tells the application how to perform a specific task. Once a generalprocedure is defined, it must be specifically invoked by the application. |
General procedure remains idle until called upon to respond to events caused by the user or by system. |
|
For Animated Presentation Click Here |
|
Why create general procedures? |
|
One reason is that several different event procedures might need the same task repeatedly. |
A good programming strategy is to put common statements in a separate procedure (a general procedure). An event procedure calls that general procedure. |
This eliminates the need to duplicate code and also makes the application easier to maintain. |
|
Example: |
|
Suppose you display some message after each event code execution then you will useevent procedure. |
|
For adding procedure to your application follow the steps given below: |
|
(1) Go to Tools menu and select Add Procedure form. Or, Use key combination Alt+T+P. |
|
The window will be shown like this: |
|
 |
|
(2) Write name of procedure Display in the name Box and select type Sub according to figure given above. |
|
 |
|
(3) Write code for display() after Clicking OK in the previous Step.The code Window will display like this: |
|
 |
|
(4) Now call procedure in any event associated your application. |
|
 |
|
( Calling Procedure display() ) |
|
|
|
|
Procedure |
|
Function Procedures |
|
Function Procedures return values. you can use Function statement to write your ownFunction procedures. |
|
|
Like a Sub procedure, a Function procedure is a separate procedure that can take arguments, perform a series of statements, and change the value of its arguments. |
|
Syntax: |
|
[Private|Public][Static]Function procedurename (arguments) [As type] |
statements |
End Function |
|
Note that: |
|
1.You call a function by including the function procedure name and arguments on the right side of a larger statement or expression (returnvalue = function()). |
|
2.Function procedures have data types, just as variables. This determines the type of the return value. |
|
3.You return a value by assigning it to the procedurename itself. |
|
Example: |
|
Public function Addition(a,b) |
Addition=a+b |
End Function |
|
Here Function addition is declared .It takes two arguments and then add these two values. It returns sum of two values. |
|
For Animated Presentation Click Here |
|
|
|
|
Procedure |
|
Property Procedures |
|
Property procedures are the procedures that return values and also assign values to the property of objects. |
Visual Basic provides three kinds of property procedures, as described in the following table. |
|
Procedure | Procedure |
Property Get | Returns the value of a property. |
Property Let | ets the value of a property. |
Property Set | Sets the value of an object property. |
|
|
Property procedures are defined in class modules. |
|
Note that: |
If you need to perform a task each time then you need to use a property procedure. Once it is defined, it executes automatically without needing an explicit call each time. |
|
|
|
|
Functions |
|
Visual Basic Functions |
|
A function is a preprogrammed calculation .It can be carried out on request from any point in a Visual Basic program. |
A function takes one or more arguments and returns a single value and it can be included in an expression. |
|
Argument: |
It is a value on which a function or procedure operates. |
For example, in the Visual Basic statement Str(10), |
number 10 is the argument. |
|
Visual Basic includes built-in functions like Sqr, Cos or Chr etc.We are discussing some of them here: |
|
(1) Sqr() |
Returns a Double specifying the square root of a number. |
|
Syntax |
Sqr(number) |
|
(2) ABS() |
Returns the absolute, positive value of the given numeric expression. |
|
Syntax |
ABS(numeric_expression) |
|
Arguments |
numeric_expression |
Is an expression of the exact numeric or approximate numeric data type category. |
Return Types |
Returns same type as numeric_expression. |
|
(3) POWER() |
Returns value of given expression to the specified power. |
|
Syntax |
POWER(numeric_expression, y) |
Arguments |
numeric_expression |
Is an expression of the exact numeric or approximate numeric data type category. |
y Is the power to which to raise numeric_expression. y can be an expression of the exact numeric or approximate numeric data type category. |
|
(4) FLOOR () |
Returns largest integer less than or equal to the given numeric expression. |
|
Syntax |
FLOOR(numeric_expression) |
Arguments |
numeric_expression |
Is an expression of exact numeric or approximate numeric data type category. Return Types |
Returns the same type as numeric_expression. |
|
For Example: |
FLOOR(122.45), Return value=122 |
FLOOR(-123.45) ,Return value=124 |
|
(5) Round() |
Returns a numeric expression, rounded to the specified length or precision. |
|
Syntax |
ROUND (numeric_expression, length) |
Arguments |
numeric_expression |
Is an expression of the exact numeric or approximate numeric data type category. |
length Is the precision to which numeric_expression is to be rounded. length must be int. |
When length is a positive number, numeric_expression is rounded to the number of decimal places specified by length. |
When length is a negative number, numeric_expression is rounded on the left side of the decimal point, as specified by length. |
Return Types |
Returns the same type as numeric_expression. |
|
For Example: |
Round(123.4545,2) ,Return Value=123.4500 |
|
(6) SQUARE() |
Returns the square of given expression. |
|
Syntax |
SQUARE(float_expression) |
Arguments |
float_expression |
Is an expression of type float. |
Return Types |
float |
|
(7) PI () |
Returns the constant value of PI. |
|
Syntax |
PI() |
Return Types |
float |
|
(8) SIGN () |
Returns the positive (+1), zero (0), or negative (-1) sign of the given expression. |
|
Syntax |
SIGN(numeric_expression) |
Arguments |
numeric_expression |
Is an expression of the exact numeric or approximate numeric data type category. |
Return Types |
Float |
|
(9) LOG10 () |
Returns the base-10 logarithm of the given float expression. |
|
Syntax |
LOG10(float_expression) |
Arguments |
float_expression |
Is an expression of the float data type. |
Return Types |
Float |
|
(10) RAND () |
Returns a random float value between 0 and 1. |
|
Syntax |
RAND([seed]) |
Arguments |
seed |
Is an integer expression (int) giving the starting value. |
Return Types |
float |
|
|
|
|
|
|
|
|
No comments:
Post a Comment