Royal University of Phnom Penh Visual Basic Programming
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 II. General Procedure I. II. Function Procedure Sub Procedure III. Event Procedure IV. Parameter Declaration I. III. IV. By. Val keyword By. Ref keyword Optional keyword Param. Array keyword Method VI. Conclusion V.
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 Functon. Name([parameterlist]) As returntype Function Statement Return Value ‘Function. Name=value End Function
Function Procedure (continue) �Function. Name is the name of the function you’re creating. �As returntype is a pair of keywords that specifies the function return type. � Parameterlist is 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 By. Val 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 any function statement after the return statement, these won’t be executed. In order to return value to calling procedure we can also used: Function. Name=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 btn. Calculation_Click(By. Val sender As System. Object, By. Val e As System. Event. Args) Handles btn. Calculation. Click Dim n As Integer n = Convert. To. Int 32(txt. Input. Text) Message. Box. Show("Factorial(" & n & ")=" & Factorial(n), "test", ) End Sub Function Factorial(By. Val 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: Sub Procedure. Name([parameterlist]) procedure Statement End Sub
Sub Procedure(continue) �Pocedure. Name is the name of the Sub procedure you’re creating. �Parameter. List 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 By. Val 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]) -Call is 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 btn. Welcome_Click(By. Val sender As System. Object, By. Val e As System. Event. Args) Handles btn. Welcome. Click Dim name As String name = txt. Input. Text Greeting(name) 'Call Greeting(name) End Sub Greeting(By. Val name As String) Message. Box. 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 Button 1 object is clicked, the Button 1_Click event procedure is executed.
Event Procedure(continue) �Example(source code) Private Sub Button 1_Click(By. Val sender As System. Object, By. Val e As System. Event. Args) Handles Button 1. Click Message. Box. Show("This is an event procedure that is executed by user click on button 1") 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] [By. Val | By. Ref] [Param. Array] 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 [By. Val | By. Ref] parametername As datatype = defaultvalue
1. By. Val keyword • Parameter which is declared by keyword By. Val cannot return the value to variable in Argument in calling code.
By. Val keyword(continue) �Example: (Source code) Private Sub cmd. OK_Click(By. Val sender As System. Object, By. Val e As System. Event. Args) Handles cmd. OK. Click Dim x As Integer x = 10 Sum(x) Message. Box. Show("now x=" & x) End Sub Sum(By. Val a As Integer) a = a + 20 End Sub
By. Val keyword(continue) � Example: (Result) Value of x is still equal 10
By. Ref keyword • Parameter which is declared by keyword By. Ref need to return the value to variable in Argument in calling code.
By. Ref keyword(continue) �Example: (Source code) Private Sub cmd. OK_Click(By. Val sender As System. Object, By. Val e As System. Event. Args) Handles cmd. OK. Click Dim x As Integer x = 10 Sum(x) Message. Box. Show("now x=" & x) End Sub Sum(By. Ref a As Integer) a = a + 20 End Sub
By. Ref 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 cmd. OK_Click(By. Val sender As System. Object, By. Val e As System. Event. Args) Handles cmd. OK. Click Dim x As Integer x = Add 10() ‘x=Add 10(10) Message. Box. Show("now x=" & x) End Sub Function Add 10(Optional By. Val i As Integer = 1) As Integer Dim s As Integer s = i + 10 Return s End Function
Optonal keyword(continue) � Example: (Result)
4. Param. Array keyword �Param. Array keyword: Specifies that a procedure parameter takes an optional array of elements of the specified type. Param. Array can be used only on the last parameter of a parameter list. Param. Array allows you to pass an arbitrary number of arguments to the procedure. A Param. Array parameter is always declared using By. Val. You can supply one or more arguments to a Param. Array parameter by passing an array of the appropriate data type, a comma-separated list of values, or nothing at all.
Param. Array keyword(continue) �Example: (Source code) Private Sub cmd. OK_Click(By. Val sender As System. Object, By. Val e As System. Event. Args) Handles cmd. OK. Click Dim x As Integer x = Sum(1, 2, 3, 4) ‘x = Sum(1, 2, ) , x = Sum() Message. Box. Show("now x=" & x) End Sub Function Sum(By. Val Param. Array a() As Integer) Dim s As Integer For i As Integer = 0 To UBound(a) s = s + a(i) Next Return s End Function
Param. Array 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: Class Math Method Description Example Max(x, y) Returns the larger of x and y Max(3. 4, 5. 6) is 5. 6 Pow(x, y) Returns a specified number raised to the specified power. Pow(2. 0, 7. 0) is 128. 0 Sqrt(x) Returns the square root of a specified number. Sqrt(9. 0) is 3. 0
Method(continue) Class Method Description Example To. String() Converts the value of this instance to a String. Dim a As Integer=10 a. To. String() is “ 10” To. Upper() Returns a copy of this String converted to uppercase. Dim s As String =“rupp” s. To. Upper() is “RUPP” Remove(x) Deletes a specified number of characters from this instance. Dim s As String=“RUPP” s. Remove(2) is “RU” String
Method(continue) Class Method Description Example Binary. Search(Array, Target) Searches a one-dimensional sorted Array for a value, using a binary search algorithm. Dim instance() As Integer = {1, 2, 3, 4, 5} Dim result As Integer result = Array. Binary. Search(instance, 4) is 3 Clear(Array, index, length) Sets a range of elements in the Array to zero Dim instance() As Integer = {1, 2, 3, 4, 5} Array. Clear(instance , 1, 2) is 1, 0, 0, 4, 5 Copy(S_array, D_array, length) Copies a range of elements from an Array starting at the first element and pastes them into another Array starting at the first element. Dim instance() As Integer = {1, 2, 3, 4, 5} Dim dest(4) As Integer Array. Copy(instance, dest, 3) is 1, 2, 3, 0, 0 Array
Method(continue) Class Method Description Example Day(Date. Value) Returns an Integer value from 1 through 31 representing the day of the month. Day(#12/3/1988#) is 3 Week. Day(Datevalu e, Day. Of. Week) Returns an Integer value containing a number representing the day of the week. Dim Date. Value As Date. Time Dim return. Value As Integer Dim dayofweek As First. Day. Of. Week Date. Value = #11/12/2010# return. Value = Date. And. Time. Weekday(Date. Value, dayofweek) is 6 Year(Date. Value) Returns an Integer value from 1 through 9999 representing the year. Day(#12/3/1988#) is 1988 Date. And. Ti me Class
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.
THANKS FOR YOUR ATTENTION
- Slides: 44