440 likes | 565 Views
Royal University of Phnom Penh. Visual Basic Programming Using VB.NET TOPIC: Procedures and Methods Lecturer By: KEAN TAK Group II: 1.CHEA BORA 2.BAN VATHANA 3.SEA SITHDARA 4.SAN REAKSMEY 5.CHAB BRIYA 6.NHEM SREYNEANG 7.KY PISETH. Contents. Procedure
E N D
Royal University of Phnom Penh Visual Basic Programming Using VB.NET TOPIC: Procedures and Methods Lecturer By: KEAN TAK Group II:1.CHEA BORA 2.BAN VATHANA 3.SEA SITHDARA 4.SAN REAKSMEY 5.CHAB BRIYA 6.NHEM SREYNEANG 7.KY PISETH
Contents • Procedure • General Procedure • Function Procedure • Sub Procedure • Event Procedure • Parameter Declaration • ByVal keyword • ByRef keyword • Optional keyword • ParamArray keyword • Method • Conclusion
I.PROCEDURE • Procedures provide a way to group a set of related statements to perform a task. • There are two types of Procedure: • General Procedure • Event Procedure
II. General Procedure Visual Basic includes two primary types of General procedures: Sub Procedure and Function Procedure.
1.Function Procedure A Function procedure is a group of statements located between a Function statement and an End Function statement. Function procedure are called by name from event procedures or other procedures. Often used for calculations, function procedures can receive arguments and always return a value in the function name.
Function Procedure (continue) • You can define a Function procedure in a module, class, or structure. It is Public by default, which means you can call it from anywhere in your application that has access to the module, class, or structure in which you defined it. • A Function procedure can take arguments, such as constants, variables, or expressions, which are passed to it by the calling code.
Function Procedure (continue) Function Syntax The basic syntax of a function is as follow: Function FunctonName([parameterlist]) As returntype Function Statement Return Value ‘FunctionName=value End Function
Function Procedure (continue) FunctionName is the name of the function you’re creating. As returntype is a pair of keywords that specifies the function return type. Parameterlistis a list of optional arguments ( separated by commas) to be used in the function. Each argument should also be declared as a specific type. (By default, Visual Basic adds the ByVal keyword to each argument, indicating that a copy of the data is passed to the function through this argument but that any changes to the arguments won’t be returned to the calling routine.)
Function Procedure (continue) Function statement is a block of statements that accomplishes the work of the function. Return is a statement which used to return a value to the calling procedure. When a return statement is executed, the function is exited, so if there are any function statement after the return statement, these won’t be executed. In order to return value to calling procedure we can also used: FunctionName=Value
Function Procedure (continue) • 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.
Function Procedure (continue) The syntax for a call to a Function procedure is as follows: lvalue = functionname[(argumentlist)] If ((functionname[(argumentlist)] / 3) <= expression) Then -lvalue is a variable which get the value that returned from function. -functionname is a name of function which we want to call. -argumentlist: is used to give the value to parameter_list in function procedure
Function Procedure (continue) Example: (Source Code) Private Sub btnCalculation_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculation.Click Dim n As Integer n = Convert.ToInt32(txtInput.Text) MessageBox.Show("Factorial(" & n & ")=" & Factorial(n), "test",) End Sub Function Factorial(ByVal n As Integer) As Long Dim p As Long = 1 For i As Integer = 1 To n p *= i Next Return p 'Factorial = p End Function
Function Procedure (continue) Example: (Result) When this button is clicked, event-procedure execute and call function procedure(Factorial())to execute too.
2.Sub Procedure A Sub procedure is a group of statements located between a Sub statement and an End Sub statement. A Sub procedure is similar to a Function procedure, except that a Sub procedure doesn’t return a value associated with its name. Sub procedures are typically used to get input from the user, display or print information, or manipulate several properties associated with condition. Sub procedures can also be used to process and update variables received in an argument list during a procedure, and pass back one or more of these values to the calling program.
Sub Procedure(continue) You can define a Sub procedure in modules, classes, and structures. By default, it is Public, which means you can call it from anywhere in your application that has access to the module, class, or structure in which you defined it. A Sub procedure can take arguments, such as constants, variables, or expressions, which are passed to it by the calling code.
Sub Procedure(continue) Sub Procedure Syntax The basic syntax of a Sub procedure is as follow: SubProcedureName([parameterlist]) procedure Statement End Sub
Sub Procedure(continue) PocedureName is the name of the Sub procedure you’re creating. ParameterList is a list of optional argument( seperated by commas if there’s more than one) to be used in the Sub procedure.Each argument should also be declared as a specific type. (By default, Visual Basic adds the ByVal keyword to each argument, indicating that a copy of the data is passed to the Sub procedure through this argument but that any changes to the arguments won’t be returned to the calling routine.)
Sub Procedure(continue) Procedure statement is a block of statements that accomplishes the work of the procedure.
Sub Procedure(continue) • Calling Sub Procedure • You invoke a Sub procedure explicitly with a stand-alone calling statement. You cannot call it by using its name 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 use of the Call keyword is optional but not recommended.
Sub Procedure(continue) The syntax for a call to a Sub procedure is as follows: [Call] subname([argumentlist]) -Callis an optional keyword that used to call Sub procedure to execute. -subame is a name of Sub procedure which we want to call. -argumentlist: is used to give the value to parameter_list in Sub procedure.
Sub Procedure(continue) Example: (Source Code) Private Sub btnWelcome_Click(ByValsender As System.Object, ByVal e As System.EventArgs) Handles btnWelcome.Click Dim name As String name = txtInput.Text Greeting(name) 'Call Greeting(name) End Sub Sub Greeting(ByVal name As String) MessageBox.Show("Hello " & name & " How are you?", "Greeting") End Sub
Sub Procedure(continue) Example: (Result) When this button is clicked, event-procedure execute and call Sub procedure(Greeting()) to execute too.
III.Event Procedure Event Procedures are Sub procedures that execute in response to an event raised by user action or by an occurrence in a program. An event procedure is a block of code that’s executed when an object is manipulated in a program. For example, when the Button1 object is clicked, the Button1_Click event procedure is executed.
Event Procedure(continue) Example(source code) Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click MessageBox.Show("This is an event procedure that is executed by user click on button1") End Sub
Event Procedure(continue) Example(Result)
IV.Parameter Declaration You declare each procedure parameter similarly to how you declare a variable, specifying the parameter name and data type. You can also specify the passing mechanism, and whether the parameter is optional or a parameter array. The syntax for each parameter in the parameter list is as follows: [Optional] [ByVal | ByRef] [ParamArray] 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 = defaultvalue
1.ByVal keyword • Parameter which is declared by keyword ByValcannot return the value to variable in Argument in calling code.
ByVal keyword(continue) Example: (Source code) Private Sub cmdOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOK.Click Dim x As Integer x = 10 Sum(x) MessageBox.Show("now x=" & x) End Sub Sub Sum(ByVal a As Integer) a = a + 20 End Sub
ByVal keyword(continue) Example: (Result) Value of x is still equal 10
ByRef keyword • Parameter which is declared by keyword ByRefneed to return the value to variable in Argument in calling code.
ByRef keyword(continue) Example: (Source code) Private Sub cmdOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOK.Click Dim x As Integer x = 10 Sum(x) MessageBox.Show("now x=" & x) End Sub Sub Sum(ByRef a As Integer) a = a + 20 End Sub
ByRef keyword(continue) Example: (Result) X is changed value to 30
3.Optonal keyword Optional Keyword: Specifies that a procedure argument can be omitted when the procedure is called.
Optonal keyword(continue) Example: (Source code) Private Sub cmdOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOK.Click Dim x As Integer x = Add10() ‘x=Add10(10) MessageBox.Show("now x=" & x) End Sub Function Add10(Optional ByVali As Integer = 1) As Integer Dim s As Integer s = i + 10 Return s End Function
Optonal keyword(continue) Example: (Result)
4.ParamArray keyword • ParamArray keyword: • Specifies that a procedure parameter takes an optional array of elements of the specified type. ParamArray can be used only on the last parameter of a parameter list. • ParamArray allows you to pass an arbitrary number of arguments to the procedure. A ParamArray parameter is always declared using ByVal. • You can supply one or more arguments to a ParamArray parameter by passing an array of the appropriate data type, a comma-separated list of values, or nothing at all.
ParamArray keyword(continue) Example: (Source code) Private Sub cmdOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOK.Click Dim x As Integer x = Sum(1, 2, 3, 4) ‘x = Sum(1, 2,) , x = Sum() MessageBox.Show("now x=" & x) End Sub Function Sum(ByValParamArray a() As Integer) Dim s As Integer For i As Integer = 0 To UBound(a) s = s + a(i) Next Return s End Function
ParamArray keyword(continue) Example: (Result)
V. Method A Method is a procedure built into the class. They are a series of statements that executed when called. The following is the example of some built-in methods:
VI.Conclusion Know about syntax of Function procedure and how to call it. Know about syntax of Sub procedure how to call it. Know about Parameter Declaration in procedure. Know some methods in some classes.