1 / 44

Royal University of Phnom Penh

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

evania
Download Presentation

Royal University of Phnom Penh

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. Contents • Procedure • General Procedure • Function Procedure • Sub Procedure • Event Procedure • Parameter Declaration • ByVal keyword • ByRef keyword • Optional keyword • ParamArray keyword • Method • Conclusion

  3. 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

  4. II. General Procedure Visual Basic includes two primary types of General procedures: Sub Procedure and Function Procedure.

  5. 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.

  6. 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.

  7. 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

  8. 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.)

  9. 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

  10. 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.

  11. 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

  12. 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

  13. Function Procedure (continue) Example: (Result) When this button is clicked, event-procedure execute and call function procedure(Factorial())to execute too.

  14. 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.

  15. 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.

  16. Sub Procedure(continue) Sub Procedure Syntax The basic syntax of a Sub procedure is as follow: SubProcedureName([parameterlist]) procedure Statement End Sub

  17. 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.)

  18. Sub Procedure(continue) Procedure statement is a block of statements that accomplishes the work of the procedure.

  19. 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.

  20. 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.

  21. 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

  22. Sub Procedure(continue) Example: (Result) When this button is clicked, event-procedure execute and call Sub procedure(Greeting()) to execute too.

  23. 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.

  24. 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

  25. Event Procedure(continue) Example(Result)

  26. 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

  27. 1.ByVal keyword • Parameter which is declared by keyword ByValcannot return the value to variable in Argument in calling code.

  28. 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

  29. ByVal keyword(continue) Example: (Result) Value of x is still equal 10

  30. ByRef keyword • Parameter which is declared by keyword ByRefneed to return the value to variable in Argument in calling code.

  31. 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

  32. ByRef keyword(continue) Example: (Result) X is changed value to 30

  33. 3.Optonal keyword Optional Keyword: Specifies that a procedure argument can be omitted when the procedure is called.

  34. 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

  35. Optonal keyword(continue) Example: (Result)

  36. 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.

  37. 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

  38. ParamArray keyword(continue) Example: (Result)

  39. 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:

  40. Method(continue)

  41. Method(continue)

  42. Method(continue)

  43. 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.

  44. THANKS FOR YOUR ATTENTION

More Related