參數清單 (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
@No__t_0 參數所需。Required for Optional parameters. 評估為參數資料類型的任何常數或常數運算式。Any constant or constant expression that evaluates to the data type of the parameter. 如果類型是 Object,或是類別、介面、陣列或結構,則預設值只能 NothingIf 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. 如果它是變數元素(宣告的變數、field、property、array 元素或 structure 元素),則呼叫程式碼可以變更它。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