参数列表 (Visual Basic)Parameter List (Visual Basic)

指定在调用过程时过程需要的参数。Specifies the parameters a procedure expects when it is called. 多个参数之间用逗号分隔。Multiple parameters are separated by commas. 下面是一个参数的语法。The following is the syntax for one parameter.

语法Syntax

[ <attributelist> ] [ Optional ] [{ ByVal | ByRef }] [ ParamArray ]
parametername[( )] [ As parametertype ] [ = defaultvalue ]

部件Parts

attributelist
可选。Optional. 应用于此参数的特性列表。List of attributes that apply to this parameter. 必须将属性列表用尖括号括起来("<" 和 ">")。You must enclose the Attribute List in angle brackets ("<" and ">").

Optional
可选。Optional. 指定在调用过程时不需要此参数。Specifies that this parameter is not required when the procedure is called.

ByVal
可选。Optional. 指定过程不能替换或重新分配调用代码中的相应参数的基础变量元素。Specifies that the procedure cannot replace or reassign the variable element underlying the corresponding argument in the calling code.

ByRef
可选。Optional. 指定过程可以采用与调用代码本身相同的方式来修改调用代码中的基础变量元素。Specifies that the procedure can modify the underlying variable element in the calling code the same way the calling code itself can.

ParamArray
可选。Optional. 指定参数列表中的最后一个参数是指定数据类型的元素的可选数组。Specifies that the last parameter in the parameter list is an optional array of elements of the specified data type. 这样,调用代码就可以将任意数量的参数传递给该过程。This lets the calling code pass an arbitrary number of arguments to the procedure.

parametername
必需。Required. 表示参数的局部变量的名称。Name of the local variable representing the parameter.

parametertype
如果 Option Strict On,则为必需。Required if Option Strict is On. 表示参数的局部变量的数据类型。Data type of the local variable representing the parameter.

defaultvalue
Optional 参数是必需的。Required for Optional parameters. 计算结果为参数的数据类型的任何常量或常量表达式。Any constant or constant expression that evaluates to the data type of the parameter. 如果类型是 Object或类、接口、数组或结构,则只能 Nothing默认值。If the type is Object, or a class, interface, array, or structure, the default value can only be Nothing.

备注Remarks

参数括在括号内并用逗号分隔。Parameters are surrounded by parentheses and separated by commas. 参数可以使用任何数据类型进行声明。A parameter can be declared with any data type. 如果不指定 parametertype,则默认为 ObjectIf you do not specify parametertype, it defaults to Object.

当调用代码调用过程时,它会将参数传递给每个必需的参数。When the calling code calls the procedure, it passes an argument to each required parameter. 有关详细信息,请参阅参数和参数之间的差异For more information, see Differences Between Parameters and Arguments.

调用代码传递给每个参数的参数是指向调用代码中的基础元素的指针。The argument the calling code passes to each parameter is a pointer to an underlying element in the calling code. 如果此元素不是变量(常量、文本、枚举或表达式),则任何代码都不能对其进行更改。If this element is nonvariable (a constant, literal, enumeration, or expression), it is impossible for any code to change it. 如果它是变量元素(已声明的变量、字段、属性、数组元素或结构元素),则调用代码可以更改它。If it is a variable element (a declared variable, field, property, array element, or structure element), the calling code can change it. 有关详细信息,请参阅可修改和不可修改参数之间的差异For more information, see Differences Between Modifiable and Nonmodifiable Arguments.

如果变量元素 ByRef传递,则该过程也可以更改。If a variable element is passed ByRef, the procedure can change it as well. 有关详细信息,请参阅按值传递参数和通过引用传递参数之间的差异For more information, see Differences Between Passing an Argument By Value and By Reference.

规则Rules

  • 括号.Parentheses. 如果指定参数列表,则必须将其括在括号中。If you specify a parameter list, you must enclose it in parentheses. 如果没有参数,仍可使用括空列表的括号。If there are no parameters, you can still use parentheses enclosing an empty list. 这可以通过阐明元素是一个过程来提高代码的可读性。This improves the readability of your code by clarifying that the element is a procedure.

  • 可选参数。Optional Parameters. 如果对参数使用 Optional 修饰符,则列表中的所有后续参数也必须是可选的,并且可以通过使用 Optional 修饰符进行声明。If you use the Optional modifier on a parameter, all subsequent parameters in the list must also be optional and be declared by using the Optional modifier.

    每个可选参数声明都必须提供 defaultvalue 子句。Every optional parameter declaration must supply the defaultvalue clause.

    有关详细信息,请参阅可选参数For more information, see Optional Parameters.

  • 参数数组。Parameter Arrays. 您必须为 ParamArray 参数指定 ByValYou must specify ByVal for a ParamArray parameter.

    不能在同一个参数列表中同时使用 OptionalParamArrayYou cannot use both Optional and ParamArray in the same parameter list.

    有关详细信息,请参阅参数数组For more information, see Parameter Arrays.

  • 传递机制。Passing Mechanism. 每个参数的默认机制都是 ByVal,这意味着该过程不能更改基础变量元素。The default mechanism for every argument is ByVal, which means the procedure cannot change the underlying variable element. 但是,如果该元素是引用类型,则该过程可以修改基础对象的内容或成员,即使它无法替换或重新分配对象本身。However, if the element is a reference type, the procedure can modify the contents or members of the underlying object, even though it cannot replace or reassign the object itself.

  • 参数名称。Parameter Names. 如果该参数的数据类型为数组,请按括号立即 parameternameIf the parameter's data type is an array, follow parametername immediately by parentheses. 有关参数名称的详细信息,请参阅已声明的元素名称For more information on parameter names, see Declared Element Names.

示例Example

下面的示例演示了一个用于定义两个参数的 Function 过程。The following example shows a Function procedure that defines two parameters.

Public Function HowMany(ByVal ch As Char, ByVal st As String) As Integer
End Function
Dim howManyA As Integer = HowMany("a"c, "How many a's in this string?")

另请参阅See also