Royal University of Phnom Penh

Post on 22-Jan-2016

35 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

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 - PowerPoint PPT Presentation

Transcript

Royal University of Phnom Penh

Visual Basic Programming Using VB.NETTOPIC: Procedures and MethodsLecturer By: KEAN TAK Group II: 1.CHEA BORA

2.BAN VATHANA3.SEA SITHDARA4.SAN REAKSMEY5.CHAB BRIYA6.NHEM SREYNEANG7.KY PISETH

Contents

I. ProcedureII. General Procedure

I. Function ProcedureII. Sub Procedure

III. Event ProcedureIV. Parameter Declaration

I. ByVal keywordII. ByRef keywordIII. Optional keywordIV. ParamArray keyword

V. MethodVI. 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 SyntaxThe 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.

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 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 SyntaxThe basic syntax of a Sub procedure is as follow:

Sub ProcedureName([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])

-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 btnWelcome_Click(ByVal sender 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 ByVal cannot 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 ByRef need 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 ByVal i 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(ByVal ParamArray 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:

Class Method Description Example

Math

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

String

ToString() Converts the value of this instance to a String.

Dim a As Integer=10a.ToString() is “10”

ToUpper() Returns a copy of this String converted to uppercase.

Dim s As String =“rupp”s.ToUpper() is “RUPP”

Remove(x) Deletes a specified number of characters from this instance.

Dim s As String=“RUPP”s.Remove(2) is “RU”

Method(continue)

Class Method Description Example

Array

BinarySearch(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.BinarySearch(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 IntegerArray.Copy(instance, dest, 3) is 1,2,3,0,0

Method(continue)

Class Method Description Example

DateAndTime Class

Day(DateValue) Returns an Integer value from 1 through 31 representing the day of the month.

Day(#12/3/1988#) is 3

WeekDay(Datevalue,DayOfWeek)

Returns an Integer value containing a number representing the day of the week.

Dim DateValue As DateTime Dim returnValue As Integer Dim dayofweek As FirstDayOfWeek DateValue = #11/12/2010# returnValue = DateAndTime.Weekday(DateValue, dayofweek) is 6

Year(DateValue) Returns an Integer value from 1 through 9999 representing the year.

Day(#12/3/1988#) is 1988

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

top related