Delegate 陳述式Delegate Statement

用來宣告委派。Used to declare a delegate. 「委派」(delegate)是參考型別,它是指型別的 Shared 方法或物件的實例方法。A delegate is a reference type that refers to a Shared method of a type or to an instance method of an object. 任何具有相符參數和傳回類型的程式都可以用來建立此委派類別的實例。Any procedure with matching parameter and return types can be used to create an instance of this delegate class. 稍後可以透過委派實例叫用此程式。The procedure can then later be invoked by means of the delegate instance.

語法Syntax

[ <attrlist> ] [ accessmodifier ] _  
[ Shadows ] Delegate [ Sub | Function ] name [( Of typeparamlist )] [([ parameterlist ])] [ As type ]  

組件Parts

詞彙Term 定義Definition
attrlist 選擇項。Optional. 適用于此委派的屬性清單。List of attributes that apply to this delegate. 以逗號分隔多個屬性。Multiple attributes are separated by commas. 您必須將屬性清單放在角括弧中(「<」和「>」)。You must enclose the Attribute List in angle brackets ("<" and ">").
accessmodifier 選擇項。Optional. 指定哪些程式碼可以存取委派。Specifies what code can access the delegate. 可以是下列其中一項:Can be one of the following:

- 公用- Public. 任何可以存取宣告委派之專案的程式碼都可以存取它。Any code that can access the element that declares the delegate can access it.
- 保護- Protected. 只有委派類別或衍生類別內的程式碼可以存取它。Only code within the delegate's class or a derived class can access it.
- Friend- Friend. 只有相同元件中的程式碼可以存取委派。Only code within the same assembly can access the delegate.
- 用。- Private. 只有宣告委派之元素內的程式碼才能存取它。Only code within the element that declares the delegate can access it.

- 受保護的 Friend僅限委派的類別、衍生類別或相同元件中的程式碼,才能存取委派。- Protected Friend Only code within the delegate's class, a derived class, or the same assembly can access the delegate.
- 只在委派的類別或相同元件中的衍生類別中,私用保護的程式碼可以存取委派。- Private Protected Only code within the delegate's class or in a derived class in the same assembly can access the delegate.
Shadows 選擇項。Optional. 表示這個委派會重新宣告並隱藏基類中名稱相同的程式設計項目,或一組多載元素。Indicates that this delegate redeclares and hides an identically named programming element, or set of overloaded elements, in a base class. 您可以使用任何其他類型遮蔽任何一種已宣告的項目。You can shadow any kind of declared element with any other kind.

無法從遮蔽項目的衍生類別內使用遮蔽的項目,除了從無法存取遮蔽項目的位置以外。A shadowed element is unavailable from within the derived class that shadows it, except from where the shadowing element is inaccessible. 例如,如果 Private 專案遮蔽基類元素,則沒有許可權存取 Private 專案的程式碼會改為存取基類元素。For example, if a Private element shadows a base class element, code that does not have permission to access the Private element accesses the base class element instead.
Sub 選擇性,但 SubFunction 必須出現。Optional, but either Sub or Function must appear. 將此程式宣告為不會傳回值的委派 Sub 程式。Declares this procedure as a delegate Sub procedure that does not return a value.
Function 選擇性,但 SubFunction 必須出現。Optional, but either Sub or Function must appear. 將此程式宣告為傳回值的委派 Function 程式。Declares this procedure as a delegate Function procedure that returns a value.
name 必要項。Required. 委派類型的名稱;遵循標準變數命名慣例。Name of the delegate type; follows standard variable naming conventions.
typeparamlist 選擇項。Optional. 此委派的型別參數清單。List of type parameters for this delegate. 多個類型參數會以逗號分隔。Multiple type parameters are separated by commas. 或者,您可以使用 InOut 泛型修飾詞,將每個型別參數宣告為 variant。Optionally, each type parameter can be declared variant by using In and Out generic modifiers. 您必須將類型清單括在括弧中,並使用 Of 關鍵字加以引進。You must enclose the Type List in parentheses and introduce it with the Of keyword.
parameterlist 選擇項。Optional. 呼叫時傳遞至程式的參數清單。List of parameters that are passed to the procedure when it is called. 您必須將參數清單括在括弧中。You must enclose the Parameter List in parentheses.
type 如果您指定 Function 程式,則為必要。Required if you specify a Function procedure. 傳回值的資料類型。Data type of the return value.

備註Remarks

@No__t_0 語句會定義委派類別的參數和傳回類型。The Delegate statement defines the parameter and return types of a delegate class. 任何具有相符參數和傳回類型的程式都可以用來建立此委派類別的實例。Any procedure with matching parameters and return types can be used to create an instance of this delegate class. 稍後可以藉由呼叫委派的 Invoke 方法,透過委派實例叫用此程式。The procedure can then later be invoked by means of the delegate instance, by calling the delegate's Invoke method.

委派可以在命名空間、模組、類別或結構層級宣告,但不能在程式內宣告。Delegates can be declared at the namespace, module, class, or structure level, but not within a procedure.

每個委派類別會為傳遞的建構函式定義物件方法的規格。Each delegate class defines a constructor that is passed the specification of an object method. 對委派建構函式的引數必須是對方法的參考或 lambda 運算式。An argument to a delegate constructor must be a reference to a method, or a lambda expression.

若要指定對方法的參考,請使用下列語法:To specify a reference to a method, use the following syntax:

AddressOf [expression.]methodnameAddressOf [expression.]methodname

expression 的編譯時期型別必須是類別或介面的名稱,而該類別或介面包含其簽章符合委派類別簽章的特定名稱方法。The compile-time type of the expression must be the name of a class or an interface that contains a method of the specified name whose signature matches the signature of the delegate class. methodname 可以是共用的方法或執行個體方法。The methodname can be either a shared method or an instance method. methodname 不是選擇性的,即使您為類別的預設方法建立了委派也一樣。The methodname is not optional, even if you create a delegate for the default method of the class.

若要指定 lambda 運算式,請使用下列語法:To specify a lambda expression, use the following syntax:

Function ([parm As type, parm2 As type2, ...]) expressionFunction ([parm As type, parm2 As type2, ...]) expression

函式的簽章必須符合委派型別的簽章。The signature of the function must match that of the delegate type. 如需 Lambda 運算式的詳細資訊,請參閱 Lambda 運算式For more information about lambda expressions, see Lambda Expressions.

如需委派的詳細資訊,請參閱委派For more information about delegates, see Delegates.

範例Example

下列範例會使用 Delegate 語句來宣告委派,以對兩個數字進行操作並傳回數位。The following example uses the Delegate statement to declare a delegate for operating on two numbers and returning a number. @No__t_0 方法會採用此類型的委派實例,並使用它來對數位配對進行操作。The DelegateTest method takes an instance of a delegate of this type and uses it to operate on pairs of numbers.

Delegate Function MathOperator( 
    ByVal x As Double, 
    ByVal y As Double 
) As Double

Function AddNumbers( 
    ByVal x As Double, 
    ByVal y As Double 
) As Double
    Return x + y
End Function

Function SubtractNumbers( 
    ByVal x As Double, 
    ByVal y As Double
) As Double
    Return x - y
End Function

Sub DelegateTest( 
    ByVal x As Double, 
    ByVal op As MathOperator, 
    ByVal y As Double 
)
    Dim ret As Double
    ret = op.Invoke(x, y) ' Call the method.
    MsgBox(ret)
End Sub

Protected Sub Test()
    DelegateTest(5, AddressOf AddNumbers, 3)
    DelegateTest(9, AddressOf SubtractNumbers, 3)
End Sub

請參閱See also