Instance members and 'Me' cannot be used within a lambda expression in structures

From within a structure, you have defined a lambda expression that refers to an instance member of the structure or uses Me. The following code illustrates these two invalid references.

Structure Structure1

    Public InstanceMember As Integer

    Public Function ExampleFun() As Integer
        '' The error is caused by use of InstanceMember.
        'Dim fun1 = Function() InstanceMember
        '' The error is caused by use of Me.
        'Dim fun2 = Function() Me.InstanceMember
        'Return fun1()
    End Function

End Structure

Error ID: BC36638

To correct this error

  • Assign the instance member to a local variable, and use the local variable in your statement.

    Public Function ExampleFunFix() As Integer
        Dim temp = InstanceMember
        Dim fun1 = Function() temp
        Return fun1()
    End Function
    

See Also

Concepts

Lambda Expressions

Reference

Me

Structure Statement