Condividi tramite


Conversione rilassata dei delegati (Visual Basic)

La conversione rilassata dei delegati consente di assegnare subroutine e funzioni a delegati o gestori di eventi anche quando le firme non sono identiche. Di conseguenza, l'associazione ai delegati diventa coerente con l'associazione già consentita per le chiamate al metodo.

Parametri e tipo di ritorno

Al posto della corrispondenza esatta della firma, la conversione rilassata richiede che vengano soddisfatte le seguenti condizioni quando Option Strict è impostato su On:

  • Una conversione verso un tipo di dati più grande deve esistere dal tipo di dati di ogni parametro delegato al tipo di dati del parametro corrispondente della funzione assegnata o Sub. Nell'esempio seguente il delegato Del1 ha un parametro , un oggetto Integer. Il parametro m nelle espressioni lambda assegnate deve avere un tipo di dati per il quale è presente una conversione più ampia da Integer, ad esempio Long o 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
    

    Le conversioni di tipo narrowing sono consentite solo quando Option Strict è impostato su 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
    
  • Una conversione verso un tipo più ampio deve esistere nella direzione opposta dal tipo restituito della funzione assegnata o Sub al tipo restituito del delegato. Negli esempi seguenti, il corpo di ogni espressione lambda assegnata deve restituire un tipo di dati che si estende a Integer perché il tipo restituito di del1 è 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)
    

Se Option Strict è impostato su Off, la restrizione di ampliamento viene rimossa in entrambe le direzioni.

' 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)

Omissione delle specifiche dei parametri

I delegati rilassati consentono anche di omettere completamente le specifiche dei parametri nel metodo che è stato assegnato.

' 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))

Si noti che non è possibile specificare alcuni parametri e omettere altri.

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

La possibilità di omettere parametri è utile in una situazione come la definizione di un gestore eventi, in cui sono coinvolti diversi parametri complessi. Gli argomenti di alcuni gestori eventi non vengono utilizzati. Al contrario, il gestore accede direttamente allo stato del controllo in cui è registrato l'evento e ignora gli argomenti. I delegati flessibili consentono di omettere gli argomenti in tali dichiarazioni quando non si creano ambiguità. Nell'esempio seguente il metodo OnClick completamente specificato può essere riscritto come 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  

Esempi di AddressOf

Le espressioni lambda vengono usate negli esempi precedenti per semplificare la visualizzazione delle relazioni tra i tipi. Tuttavia, gli stessi allentamenti sono consentiti per le assegnazioni di delegati che usano AddressOf, Handles, o AddHandler.

Nell'esempio seguente, le funzioni f1, f2, f3e f4 possono essere assegnate a 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

L'esempio seguente è valido solo quando Option Strict è impostato su 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))

Eliminazione di una funzione restituisce

La conversione delegata permissiva consente di assegnare una funzione a un Sub delegato, ignorando in modo efficace il valore di ritorno della funzione. Tuttavia, non è possibile assegnare un oggetto Sub a un delegato di funzione. Nell'esempio seguente l'indirizzo della funzione doubler viene assegnato al Sub delegato 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))

Vedere anche