A parameter is a local variable that is valid in this process. When a process with parameters is called, the combination of form parameters and real parameters is first carried out, that is, data transmission between the form parameters involved in the called process is realized. There are two methods for passing VB. NET parameters: pass by value and pass by address.
The format of each parameter is described as follows:
[{ByVal | ByRef}] [ParamArray] parameter name [()] As parameter type [= default value]
The parameter table can contain multiple parameters separated by commas.
1. PASS Parameters by value
The parameter passing by value method is default in VB. NET, And the keyword "ByVal" is used to indicate that the parameter is passed by value. When passing by value, first assign a temporary memory unit for the parameter variable, and then pass the value of the real parameter to this temporary unit. Passing by value is unidirectional. If the process changes the parameter value, it only affects the value of the temporary unit without affecting the value of the real variable. For example:
Sub Inc (ByVal I As Integer)
I = I + 1
End Sub
Sub CallInc ()
Dim X As Integer = 10
Response. Write ("X =" & X)
Inc (X)
Response. Write ("X =" & X)
End Sub
In the CallInc process, call the Inc function and pass the value 10 of the real parameter X to the I variable. The function Inc () increases the value of I by 1, but this change does not affect the value of X, therefore, the X output by CallInc is 10.
2. PASS Parameters by address
The parameter passing by address is to pass the Real Variable address of the call process to the corresponding parameter, so that the real parameter and the parameter have the same address, that is, the form participates in the shared storage unit of the real parameter. Therefore, if the value of the parameter is changed during the call, the value of the parameter is also changed. Use the "ByRef" keyword to indicate that parameters are transmitted by address. The efficiency of transferring data by address is relatively high, because no matter what type of variable is, only four bytes are transferred.
Example 3.8: exchange the values of two variables. The page shown in Figure 3-15 is displayed during program execution. In the preceding two text boxes, enter two integers and click "Submit" to display the exchanged values in the following two text boxes, as shown in figure 3-16.
Figure 3-15 Running page
In this example, a SwapByRef X, ByRef Y) subroutine is designed. The real parameter values of X and Y are exchanged using the pass-through address parameter "ByRef.
Source code is as follows Ex3-8.aspx ):
|
| Figure 3-16 running results |
3. Optional parameters
Parameters that contain the "Optional" keyword are Optional. If a parameter is optional, all parameters following this parameter must also be optional. Each optional parameter must have a default value, and the default value of the optional parameter must be a constant. The following example defines a process with optional parameters:
Sub ABC (Optional ByVal MyCountry As String = "China ")
...... 'Subroutine body statement
End Sub
When calling this process, you can choose whether to pass parameters to the process. If no parameters are passed, the process uses the default parameters. For example, if the actual parameter value is not provided for the process call ABC (), the default value "China" is used ".
4. array parameters
When defining a process, an array can be used as a parameter. The syntax format of declaring an array parameter is different from that of a common parameter.
Syntax:
[ByVal | ByRef] Name of the form parameter array () [As data type]
The data type is the element type of the parameter array. The real parameter array corresponding to the parameter array must also be an array, and the data type is consistent with the element type of the parameter array. "()" Is not required for arrays in the real parameter table. The passing of array parameters features that the form parameter array shares the storage space with the real parameter array. You can use ByVal or ByRef to pass an array. When you use ByVal, the statement in the process can change the element value of the array, but cannot change the parameter array. The ByRef method can change the parameter array.
For example, the following subroutine calculates the sum of each element of array A, and n is the maximum downlink value of the array:
Sub SumArray (A () As Integer, n As Integer)
Dim I As Integer, S As Integer = 0
For I = 0 To n
S + = A (I)
Next
End Sub
Example 3.9: The goods table shown in 3-17 is displayed. Design a sub-program DisplayGoods and use a two-dimensional array as the input parameter to display the values of each element of the Two-dimensional array. During Page_Load event processing, store the product name, quantity, and unit price data in a two-dimensional array and pass the array as a real parameter to DisplayGoods.
Figure 3-17 transmit and display table data using array parameters
Source code is as follows Ex3-9.aspx ):
Note:
1) Sub DisplayGoods (A (,) As Object), where A (,) indicates that the parameter is A two-dimensional array.
2) The program uses LBound (A, 1), UBound (A, 1), LBound (A, 2), UBound (A, 2) take the minimum and maximum values of the first and second dimensions of the array.
3) an array of the Object type is used. The Object type can be any data type, including strings and numbers.
Type.
| BibliographyPrevious sectionNext section |