Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
Expand Minimize

'Extension' attribute can be applied only to 'Module', 'Sub', or 'Function' declarations

The only way to extend a data type in Visual Basic is to define an extension method inside a standard module. The extension method can be a Sub procedure or a Function procedure. All extension methods must be marked with the extension attribute, <Extension()>, from the System.Runtime.CompilerServices namespace. Optionally, a module that contains an extension method may be marked in the same way. No other use of the extension attribute is valid.

Error ID: BC36550

To correct this error

  • Remove the extension attribute.

  • Redesign your extension as a method, defined in an enclosing module.

The following example defines a Print method for the String data type.

Imports StringUtility
Imports System.Runtime.CompilerServices
Namespace StringUtility
    <Extension()> 
    Module StringExtensions
        <Extension()> 
        Public Sub Print (ByVal str As String)
            Console.WriteLine(str)
        End Sub
    End Module
End Namespace
Show:
© 2015 Microsoft