Methods of 'System.Nullable(Of T)' cannot be used as operands of the 'AddressOf' operator


Updated: July 20, 2015

For the latest documentation on Visual Studio 2017, see Visual Studio 2017 Documentation.

A statement uses the AddressOf operator with an operand that represents a procedure of the Nullable<T> structure.

Error ID: BC32126

To correct this error

  • Replace the procedure name in the AddressOf clause with an operand that is not a member of Nullable<T>.

  • Write a class that wraps the method of Nullable<T> that you want to use. In the following example, the NullableWrapper class defines a new method named GetValueOrDefault. Because this new method is not a member of Nullable<T>, it can be applied to nullInstance, an instance of a nullable type, to form an argument for AddressOf.

    Module Module1  
        Delegate Function Deleg() As Integer  
        Sub Main()  
            Dim nullInstance As New Nullable(Of Integer)(1)  
            Dim del As Deleg  
            ' GetValueOrDefault is a method of the Nullable generic  
            ' type. It cannot be used as an operand of AddressOf.  
            ' del = AddressOf nullInstance.GetValueOrDefault  
            ' The following line uses the GetValueOrDefault method  
            ' defined in the NullableWrapper class.  
            del = AddressOf (New NullableWrapper(  
                Of Integer)(nullInstance)).GetValueOrDefault  
        End Sub  
        Class NullableWrapper(Of T As Structure)  
            Private m_Value As Nullable(Of T)  
            Sub New(ByVal Value As Nullable(Of T))  
                m_Value = Value  
            End Sub  
            Public Function GetValueOrDefault() As T  
                Return m_Value.Value  
            End Function  
        End Class  
    End Module  

AddressOf Operator
Nullable Value Types
Generic Types in Visual Basic