Udostępnij za pośrednictwem


Konwersja obiektu delegowanego obniżone (Visual Basic)

Pełnomocnik obniżone konwersji umożliwia przypisywanie Podprocedura i funkcje delegatów lub obsługi, nawet w przypadku, gdy ich podpisy nie są identyczne. W związku z tym powiązanie z delegatów staje się zgodne wiązanie już dozwolone dla wywołania metody. 

Parametry i zwracany typ

Zamiast podpisu dokładne dopasowanie, obniżone konwersji wymaga spełnienia następujących warunków podczas Option Strict jest ustawiona na On:

  • Poszerzanie konwersji musi istnieć z typ danych każdego parametru delegata odpowiedniego parametru funkcji przypisany typ danych lub Sub.W poniższym przykładzie, pełnomocnik Del1 ma jeden parametr, Integer.Parametr m w przypisanych lambda wyrażenia musi mieć typ danych, dla którego jest poszerzanie konwersji z Integer, takich jak Long lub 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
    

    Konwersji zawężającej są dozwolone tylko wtedy, gdy Option Strict jest ustawiona na 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
    
  • Poszerzanie konwersji musi istnieć w przeciwnym kierunku od zwrotu typu przypisanych funkcji lub Sub do zwracany typ delegata.W poniższych przykładach treści każdego wyrażenia lambda przypisane musi być typ danych, który rozszerza się do Integer , ponieważ typ zwrotu del1 jest 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)
    

Jeśli Option Strict jest ustawiona na Off, poszerzenie ograniczenie jest usuwany w obu kierunkach.

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

Pominięcie parametru specyfikacje

Obniżone delegatów umożliwiają również całkowicie pominąć parametr specyfikacji w metodzie przypisane:

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

Należy zauważyć, że nie można określić niektóre parametry i pominąć inne.

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

Możliwość pominięcia parametrów jest przydatne w sytuacji, takich jak określenie programu obsługi zdarzeń, gdy sprawa dotyczy kilku parametrów złożonych.Argumenty niektóre programy obsługi zdarzeń nie są używane.Zamiast tego program obsługi bezpośredni dostęp do stanu kontroli, w którym zdarzenie jest zarejestrowany i ignoruje argumenty.Obniżone delegatów umożliwiają pominąć argumenty w takich deklaracji, gdy wynik nie niejasności.W poniższym przykładzie pełni określona metoda OnClick można zapisać jako 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

Przykłady AddressOf

Wyrażenia lambda są używane w poprzednich przykładach, aby ułatwić Zobacz relacji typu.Jednak ten sam złagodzenie są dozwolone dla przypisania pełnomocnika, używające AddressOf, Handles, lub AddHandler.

W poniższym przykładzie funkcji f1, f2, f3, i f4 można wszystkie przypisane do 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

Poniższy przykład jest prawidłowy tylko wtedy, gdy Option Strict jest ustawiona na 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))

Funkcja zwraca upuszczanie

Pełnomocnik obniżone konwersji umożliwia przypisywanie funkcji do Sub obiektu delegowanego, skutecznie ignorując wartość zwracana funkcji.Jednakże nie można przypisać Sub do oddelegowania funkcji.W poniższym przykładzie adres funkcji doubler jest przypisany do Sub delegować 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))

Zobacz też

Zadania

Jak: przekazać procedur do innej procedury w języku Visual Basic

Informacje

Opcja ścisłego instrukcji

Koncepcje

Wyrażenia lambda (Visual Basic)

Rozszerzanie i zawężanie konwersji (Visual Basic)

Wnioskowanie typu lokalnego (Visual Basic)

Inne zasoby

Pełnomocnicy (Visual Basic)