Share via


寬鬆委派轉換 (Visual Basic)

即使在簽章不同的情況下,寬鬆委派轉換仍可讓您將子函數和函式指派給委派或處理常式。因此,委派的繫結就會與方法引動中允許的繫結一致。 

參數和傳回型別

寬鬆轉換不需要簽章完全相符,而要求在 Option Strict 設定為 On 時符合下列條件:

  • 擴展轉換必須存在,方向是從每個委派參數的資料型別到指派之函式或 Sub 相對應參數的資料型別。 在下列範例中,委派 Del1 有一個 Integer 參數。 所指派之 Lambda 運算式中的參數 m,其資料型別必須可從 Integer (例如 Long 或 Double) 執行擴展轉換。

    ' Definition of delegate Del1.
    Delegate Function Del1(ByVal arg As Integer) As Integer
    
    ' Valid lambda expression assignments with Option Strict on or off:
    
    ' Integer matches Integer.
    Dim d1 As Del1 = Function(m As Integer) 3
    
    ' Integer widens to Long
    Dim d2 As Del1 = Function(m As Long) 3
    
    ' Integer widens to Double
    Dim d3 As Del1 = Function(m As Double) 3
    

    只有在 Option Strict 設定為 Off 時,才允許縮小轉換。

    ' Valid only when Option Strict is off:
    
    Dim d4 As Del1 = Function(m As String) CInt(m)
    Dim d5 As Del1 = Function(m As Short) m
    
  • 反方向的擴展轉換必須存在,方向是從指派之函式或 Sub 的傳回型別到委派的傳回型別。 在下列範例中,因為 del1 的傳回型別為 Integer,每個指派之 Lambda 運算式的主體都必須評估為擴展至 Integer 的資料型別。

    ' Valid return types with Option Strict on:
    
    ' Integer matches Integer.
    Dim d6 As Del1 = Function(m As Integer) m
    
    ' Short widens to Integer.
    Dim d7 As Del1 = Function(m As Long) CShort(m)
    
    ' Byte widens to Integer.
    Dim d8 As Del1 = Function(m As Double) CByte(m)
    

如果 Option Strict 設定為 Off,則會移除雙向擴展限制。

' Valid only when Option Strict is set to Off.

' Integer does not widen to Short in the parameter.
Dim d9 As Del1 = Function(n As Short) n

' Long does not widen to Integer in the return type.
Dim d10 As Del1 = Function(n As Integer) CLng(n)

省略參數規格

寬鬆委派也可讓您在指派的方法中,完全省略參數規格:

' Definition of delegate Del2, which has two parameters.
Delegate Function Del2(ByVal arg1 As Integer, ByVal arg2 As String) As Integer
' The assigned lambda expression specifies no parameters, even though
' Del2 has two parameters. Because the assigned function in this 
' example is a lambda expression, Option Strict can be on or off.
' Compare the declaration of d16, where a standard function is assigned.
Dim d11 As Del2 = Function() 3

' The parameters are still there, however, as defined in the delegate.
Console.WriteLine(d11(5, "five"))

' Not valid.
' Console.WriteLine(d11())
' Console.WriteLine(d11(5))

請注意,您不能指定部分參數而省略其他參數。

' Not valid.
'Dim d12 As Del2 = Function(p As Integer) p

在定義事件處理常式這類牽涉到多個複雜參數的情況下,省略參數將很有幫助。 有時不會用到某些事件處理常式的引數, 而是處理常式直接存取註冊事件之控制項的狀態,並忽略引數。 寬鬆委派可讓您在不會造成模稜兩可 (Ambiguity) 的情況下,於此類宣告中省略引數。 在下列範例中,完整指定的方法 OnClick 可以重新撰寫為 RelaxedOnClick。

Sub OnClick(ByVal sender As Object, ByVal e As EventArgs) Handles b.Click
    MessageBox.Show("Hello World from" + b.Text)
End Sub

Sub RelaxedOnClick() Handles b.Click
    MessageBox.Show("Hello World from" + b.Text)
End Sub

AddressOf 範例

透過上述範例中 Lambda 運算式的用法,您可以輕易看出型別關聯性。 不過,您也可以針對使用 AddressOf、Handles 或 AddHandler 的委派指派,使用相同的寬鬆原則。

在下列範例中,f1、f2、f3 和 f4 函式都可指派至 Del1。

' Definition of delegate Del1.
Delegate Function Del1(ByVal arg As Integer) As Integer
' Definitions of f1, f2, f3, and f4.
Function f1(ByVal m As Integer) As Integer
End Function

Function f2(ByVal m As Long) As Integer
End Function

Function f3(ByVal m As Integer) As Short
End Function

Function f4() As Integer
End Function
' Assignments to function delegate Del1.

' Valid AddressOf assignments with Option Strict on or off:

' Integer parameters of delegate and function match.
Dim d13 As Del1 = AddressOf f1

' Integer delegate parameter widens to Long.
Dim d14 As Del1 = AddressOf f2

' Short return in f3 widens to Integer.
Dim d15 As Del1 = AddressOf f3

下列範例只有在 Option Strict 設定為 Off 時才有效。

' If Option Strict is Off, parameter specifications for f4 can be omitted.
Dim d16 As Del1 = AddressOf f4

' Function d16 still requires a single argument, however, as specified
' by Del1.
Console.WriteLine(d16(5))

' Not valid.
'Console.WriteLine(d16())
'Console.WriteLine(d16(5, 3))

置放函式傳回

寬鬆委派轉換可讓您將函式指派給 Sub 委派,進而有效地忽略函式的傳回值。 不過,您無法將 Sub 指派給函式委派。 在下列範例中,doubler 函式的位址已指派給 Sub 委派 Del3。

' Definition of Sub delegate Del3.
Delegate Sub Del3(ByVal arg1 As Integer)

' Definition of function doubler, which both displays and returns the
' value of its integer parameter.
Function doubler(ByVal p As Integer) As Integer
    Dim times2 = 2 * p
    Console.WriteLine("Value of p: " & p)
    Console.WriteLine("Double p:   " & times2)
    Return times2
End Function
' You can assign the function to the Sub delegate:
Dim d17 As Del3 = AddressOf doubler

' You can then call d17 like a regular Sub procedure.
d17(5)

' You cannot call d17 as a function. It is a Sub, and has no 
' return value.
' Not valid.
'Console.WriteLine(d17(5))

請參閱

工作

HOW TO:在 Visual Basic 中將程序傳遞至其他程序

參考

Option Strict 陳述式

概念

Lambda 運算式 (Visual Basic)

擴展和縮小轉換 (Visual Basic)

區域型別推斷 (Visual Basic)

其他資源

委派 (Visual Basic)