powerpoint presentation on procedure, function & module in visual basic
TRANSCRIPT
![Page 1: POWERPOINT PRESENTATION ON PROCEDURE, FUNCTION & MODULE IN VISUAL BASIC](https://reader038.vdocument.in/reader038/viewer/2022110101/56649e615503460f94b5d1a7/html5/thumbnails/1.jpg)
POWERPOINT PRESENTATION ON
PROCEDURE, FUNCTION & MODULE IN VISUAL BASIC
![Page 2: POWERPOINT PRESENTATION ON PROCEDURE, FUNCTION & MODULE IN VISUAL BASIC](https://reader038.vdocument.in/reader038/viewer/2022110101/56649e615503460f94b5d1a7/html5/thumbnails/2.jpg)
ProceduresA procedure is a block of Visual Basic statements enclosed by a declaration statement (Sub, Function,Get, Set) and a matching End declaration. All executable statements in Visual Basic must be within some procedure.
Why to Create a Procedure
It allow you to break your program into discrete logical units, each of which can be debug more easily than an entire program without procedures.
It can also use as a building block for other programs.
If you have code that performs the same task in different places, you can write the task once as a procedure and then call it from different places in your code.
![Page 3: POWERPOINT PRESENTATION ON PROCEDURE, FUNCTION & MODULE IN VISUAL BASIC](https://reader038.vdocument.in/reader038/viewer/2022110101/56649e615503460f94b5d1a7/html5/thumbnails/3.jpg)
Types of Procedures1. Sub Procedures A Sub procedure is a series of Visual
Basic statements enclosed by the Sub and End Sub statements. The Sub procedure performs a task and then returns control to the calling code, but it does not return a value to the calling code.
It is Public by default, which means you can call it from anywhere in your application. It can take arguments, which are passed to it by the calling code.
Types of Sub Procedure They are of 2 types:
a) General ProcedureIt tells the application how to perform a specific task. It must be invoked by an application.
b) Event ProcedureEvent procedures execute in response to an event raised by user action or by an occurrence in a program such as click event of an object.
![Page 4: POWERPOINT PRESENTATION ON PROCEDURE, FUNCTION & MODULE IN VISUAL BASIC](https://reader038.vdocument.in/reader038/viewer/2022110101/56649e615503460f94b5d1a7/html5/thumbnails/4.jpg)
Parts of a Function Procedure A Function Procedure has these parts:
Visibility Public, Friend or Private
Procedure Type Sub, Function, Property Let, Property Get, Property Set
Name Anything you like as long as it starts with a letter and contains only letters numbers and underscores.
Argument List a list of the items of data that the procedure needs,
Return Type if the type is Function or Property Get this tells the compiler what kind of thing will be returned, for instance, Double or String.
Body all the statements that do the work.
![Page 5: POWERPOINT PRESENTATION ON PROCEDURE, FUNCTION & MODULE IN VISUAL BASIC](https://reader038.vdocument.in/reader038/viewer/2022110101/56649e615503460f94b5d1a7/html5/thumbnails/5.jpg)
How to: Create a ProcedureYou enclose a procedure between a starting declaration statement (Sub or Function) and an ending declaration statement (End Sub or End Function). All the procedure's code lies between these statements.
Declaration Syntax of General Procedure [ modifiers ] Sub subname [( parameterlist )]
' Statements of the Sub procedure.
End Sub
Declaration Syntax of Event Procedure [ modifiers]Sub objectname_eventname [(parameterlist )]
' Statements of the Sub procedure.
End Sub
The modifiers can specify access level (Public, Private)
![Page 6: POWERPOINT PRESENTATION ON PROCEDURE, FUNCTION & MODULE IN VISUAL BASIC](https://reader038.vdocument.in/reader038/viewer/2022110101/56649e615503460f94b5d1a7/html5/thumbnails/6.jpg)
ParametersIn most cases, a procedure needs to operate on different data each time you call it. You can pass this information to the procedure as part of the procedure call. The procedure defines zero or more parameters, each of which represents a value it expects you to pass to it.
By default, all parameters to a VB procedure are passed by reference. When you pass by value, you'll get saved from subtle bugs.
Passing by value (ByVal): The procedure gets a copy of the actual value of the argument.
Function MultiplyByTwo(ByVal x As Integer) As Integer
Passing by reference (ByRef): The procedure gets access to the argument's actual memory address.
Function MultiplyByTwo(ByRef x As Integer) As Integer
When a ByRef parameter is used, the value of the passed argument can be permanently changed by the procedure.
![Page 7: POWERPOINT PRESENTATION ON PROCEDURE, FUNCTION & MODULE IN VISUAL BASIC](https://reader038.vdocument.in/reader038/viewer/2022110101/56649e615503460f94b5d1a7/html5/thumbnails/7.jpg)
You declare each procedure parameter similarly to how you declare a variable, specifying the parameter name and data type. The syntax for each parameter in the parameter list is as follows:
[Optional] [ByVal|ByRef] parametername As datatype
If the parameter is optional, you must also supply a default value as part of its declaration. The syntax for specifying a default value is as follows:
Optional [ByVal | ByRef] parametername As datatype = defaultval
Calling Procedure
You invoke a Sub procedure explicitly with a stand-alone calling statement , Call keyword which is optional.
The syntax for a call to a Sub procedure
[Call] subname [( argumentlist )]
Parameter Declaration
![Page 8: POWERPOINT PRESENTATION ON PROCEDURE, FUNCTION & MODULE IN VISUAL BASIC](https://reader038.vdocument.in/reader038/viewer/2022110101/56649e615503460f94b5d1a7/html5/thumbnails/8.jpg)
Example of Sub Procedure
Private Sub sum(a,b)
c=a+b
Print c
End Sub
Private Sub command1_click()
Call sum(10,20)
Or
Sum(10,20)
End Sub
![Page 9: POWERPOINT PRESENTATION ON PROCEDURE, FUNCTION & MODULE IN VISUAL BASIC](https://reader038.vdocument.in/reader038/viewer/2022110101/56649e615503460f94b5d1a7/html5/thumbnails/9.jpg)
2. Function Procedures
A Function procedure is a series of Visual Basic statements enclosed by the Function and End Function statements. The Function procedure performs a task and then returns control to the calling code. When it returns control, it also returns a value to the calling code.
Declaration Syntax of Function Procedure
The syntax for declaring a Function procedure is as follows:
[modifiers] Function funcname [(paramlist)] As returntype
' Statements of the Function procedure.
functionname = expression
Return expression
End Function
Data TypeEvery Function procedure has a data type. This data type is specified by the As clause in the Function statement, and it determines the data type of the value the function returns to the calling code.
Returns control to the code that called a Function, Sub, Get, Set, or Operator procedure
![Page 10: POWERPOINT PRESENTATION ON PROCEDURE, FUNCTION & MODULE IN VISUAL BASIC](https://reader038.vdocument.in/reader038/viewer/2022110101/56649e615503460f94b5d1a7/html5/thumbnails/10.jpg)
Calling Function Procedure
You invoke a Function procedure by including its name and arguments either on the right side of an assignment statement or in an expression. You must provide values for all arguments that are not optional, and you must enclose the argument list in parentheses. If no arguments are supplied, you can optionally omit the parentheses.
The syntax for a call to a Function procedure :
[ modifiers]Sub objectname_eventname [(parameterlist )]
varname = functionname [( argumentlist )]
End Sub
![Page 11: POWERPOINT PRESENTATION ON PROCEDURE, FUNCTION & MODULE IN VISUAL BASIC](https://reader038.vdocument.in/reader038/viewer/2022110101/56649e615503460f94b5d1a7/html5/thumbnails/11.jpg)
Example of Function Procedure
Public Function Sum(ByRef a As Double, ByRef b As Double) As Double
Sum = a + b
Return sum
End Function
Private Sub command1_click()
csum= sum(10,20)
Print csum
End Sub
![Page 12: POWERPOINT PRESENTATION ON PROCEDURE, FUNCTION & MODULE IN VISUAL BASIC](https://reader038.vdocument.in/reader038/viewer/2022110101/56649e615503460f94b5d1a7/html5/thumbnails/12.jpg)
3. Property Procedures A property procedure is a series of Visual Basic statements that manipulate a custom property on a module, class, or structure.
Visual Basic provides for the following property procedures:
•A Get procedure returns the value of a property. It is called when you access the property in an expression.
•A Set procedure sets a property to a value, including an object reference. It is called when you assign a value to the property.
You usually define property procedures in pairs, using the Get and Set statements, but you can define either procedure alone if the property is read-only (Get Statement) or write-only (Set Statement (Visual Basic)).
![Page 13: POWERPOINT PRESENTATION ON PROCEDURE, FUNCTION & MODULE IN VISUAL BASIC](https://reader038.vdocument.in/reader038/viewer/2022110101/56649e615503460f94b5d1a7/html5/thumbnails/13.jpg)
Syntax of Property Procedure
The syntax for declaring a property and its procedures :
[Default] [modifiers] Property propertyname[(parameterlist)] As datatype
[accesslevel]
Get
' Statements of the Get procedure.
' The following statement returns expression as the property's value.
Return expression
End Get
[accesslevel] Set[(ByVal newvalue As datatype)]
' Statements of the Set procedure.
' The following statement assigns newvalue as the property's value.
lvalue = newvalue
End Set
End Property
![Page 14: POWERPOINT PRESENTATION ON PROCEDURE, FUNCTION & MODULE IN VISUAL BASIC](https://reader038.vdocument.in/reader038/viewer/2022110101/56649e615503460f94b5d1a7/html5/thumbnails/14.jpg)
Example of Property ProcedureDim firstName, lastName As String Property fullName() As String
Get
If lastName = "" Then
Return firstName
Else
Return firstName & " " & lastName
End If
End Get
Set(ByVal Value As String)
Dim space As Integer
If space < 0
Then firstName = Value
lastName = ""
else
firstName = Value.Substring(0, space)
lastName = Value.Substring(space + 1)
End If
End Set
End Property
![Page 15: POWERPOINT PRESENTATION ON PROCEDURE, FUNCTION & MODULE IN VISUAL BASIC](https://reader038.vdocument.in/reader038/viewer/2022110101/56649e615503460f94b5d1a7/html5/thumbnails/15.jpg)
Modules
Modules are the code container. Visual Basic supports 3 types of modules:
1. Form Module : It consists of small piece of code called
Procedures.
It includes the description, settings and properties related to a single form.
Its extension is .frm.
![Page 16: POWERPOINT PRESENTATION ON PROCEDURE, FUNCTION & MODULE IN VISUAL BASIC](https://reader038.vdocument.in/reader038/viewer/2022110101/56649e615503460f94b5d1a7/html5/thumbnails/16.jpg)
2. Standard Module:
It's better to separate all your Functions and Subs and put them somewhere else - in something called a Module. That way, we can use them in other projects.
It does not have a GUI and contains only code.
It is a container for procedures and declarations commonly used by within the application.
They contain global and ,module level declarations of variables.
Its extension is .bas.
![Page 17: POWERPOINT PRESENTATION ON PROCEDURE, FUNCTION & MODULE IN VISUAL BASIC](https://reader038.vdocument.in/reader038/viewer/2022110101/56649e615503460f94b5d1a7/html5/thumbnails/17.jpg)
How to add a standard module? start a new project.
Add a button to you new form.
To add a Module in project, click Project from the menubar.
From the from down menu, click on "Add Module":
From the from down menu, click on "Add Module":You'll should see a blank window, with this code in it:
![Page 18: POWERPOINT PRESENTATION ON PROCEDURE, FUNCTION & MODULE IN VISUAL BASIC](https://reader038.vdocument.in/reader038/viewer/2022110101/56649e615503460f94b5d1a7/html5/thumbnails/18.jpg)
Example of Standard Module
MODULE
FORM
CALLING OF MODULE
![Page 19: POWERPOINT PRESENTATION ON PROCEDURE, FUNCTION & MODULE IN VISUAL BASIC](https://reader038.vdocument.in/reader038/viewer/2022110101/56649e615503460f94b5d1a7/html5/thumbnails/19.jpg)
3. Class ModuleThey are the foundation of Object-oriented programming in Visual Basic.
It is used to create new objects.
They have no visible user interface.
Its extension is .cls.
![Page 20: POWERPOINT PRESENTATION ON PROCEDURE, FUNCTION & MODULE IN VISUAL BASIC](https://reader038.vdocument.in/reader038/viewer/2022110101/56649e615503460f94b5d1a7/html5/thumbnails/20.jpg)
How to add a class module? To add a Class Module in project, click Project from the
menubar.
From the from down menu, click on "Add ClassModule":
From the Add Class Module window, click on “Open":You'll should see a blank window, with this code in it: