Udostępnij za pośrednictwem


Option Strict On does not allow narrowing in implicit type conversions between method '<methodname>' and delegate '<delegatename>'

With Option Strict on, you cannot have a narrowing conversion between the data type of a parameter in a delegate and the corresponding parameter of a function or Sub assigned to a variable of that delegate type. For example, function delegate Del has one parameter of type Integer, and functions Conversion1, Conversion2, and Conversion3 have one parameter of different numeric types.

Delegate Function Del(ByVal p As Integer) As String

Function Conversion1(ByVal n As Integer) As String
    Return "Valid"
End Function

Function Conversion2(ByVal n As Long) As String
    Return "Valid"
End Function

Function Conversion3(ByVal n As Short) As String
    Return "Not valid"
End Function

Because there is a widening conversion from Integer to Integer and to Long, the following assignments are valid.

' Valid.
Dim funDel1 As Del = AddressOf Conversion1
Dim funDel2 As Del = AddressOf Conversion2

The conversion from Integer to Short is a narrowing conversion. Therefore, the following assignment is not valid.

' Not valid.
Dim funDel3 As Del = AddressOf Conversion3

Error ID: BC36663

To correct this error

  • Change the data type of the parameter in the delegate or the method so that the required widening relationship exists.

See Also

Concepts

Relaxed Delegate Conversion

Widening and Narrowing Conversions

Delegates and the AddressOf Operator

Other Resources

Delegates in Visual Basic