Add Imports Validation Error (Extension Methods) Dialog Box

The Add Imports Validation Error (Extension Methods) dialog box appears when the compiler determines that adding an Imports statement to define an unrecognized type name would change the meaning of other code in your file. Specifically, the following events have occurred.

  1. The compiler reports that a type name in your code is undefined.

  2. The type name is defined in your project or in an assembly that is referenced from your project, but the definition is in a namespace or namespace element, such as a class or a module, that is out of scope.

  3. From a list of options suggested by Smart Compile Auto Correction, you choose to fix the problem by importing the namespace or namespace element.

  4. The compiler checks the file and determines that adding the Imports statement will change the binding of other variables or methods in the file.

  5. Because at least one of the binding conflicts includes an extension method, adding the Imports statement is not supported by the compiler.

The Add Imports Validation Error (Extension Methods) dialog box contains the following options for fixing the problem.

  • Click OK to correct typeName by changing it to namespace.typeName
    Changes the undefined type name to namespace.typeName to specify the location of the type name's definition. This changes only the current instance of the undefined type name. The namespace is not imported.

  • Cancel
    Make no changes and close the dialog box.

Example

In a Visual Basic Console Application project named ConsoleApplication1, the following code causes an error because the definition of class C2 is not in scope when it is used in Main.

Imports System.Runtime.CompilerServices
Imports ConsoleApplication1.Ns1

Class Base
End Class

' Namespace Ns1 defines one class, C1.
Namespace Ns1

    Public Class C1
    End Class

    Module M1

        <Extension()> _
        Sub exampleSub(ByVal caller As Base)
            Console.WriteLine("exampleSub in Ns1")
        End Sub

    End Module
End Namespace

' Namespace Ns2 defines two classes, C1 and C2
Namespace Ns2

    Public Class C2
    End Class

    Module M2

        <Extension()> _
        Sub exampleSub(ByVal caller As Base)
            Console.WriteLine("exampleSub in Ns2")
        End Sub

    End Module
End Namespace

Module Module1

    Sub Main()
        Dim a As New C1
        ' The following line will show an error for C2. C2 is
        ' defined in namespace Ns2, but only Ns1 is imported.
        Dim b As New C2
        Dim c As New Base
        c.exampleSub()
    End Sub

End Module

An error occurs in Sub Main when C2 is referenced. The error reports that C2 is an unrecognized type. Rest the mouse pointer over C2 and click the Error Correction Options exclamation point. You will see two options for fixing the problem:

  • Import ConsoleApplication1.Ns2

  • Change 'C2' to 'Ns2.C2'

Error Correction Options

Shows Error Correction options.

If you choose to import ConsoleApplication1.Ns2, the compiler detects that the import causes a conflict with extension method exampleSub, which is defined in both Ns1 and Ns2. At that point, you can choose either to qualify C2 or to click Cancel in the dialog box and return to your code to make some other change.

Add Imports Validation Error dialog box

Shows choices in the dialog box.

See Also

Concepts

Resolving a Reference When Multiple Variables Have the Same Name

Extension Methods (Visual Basic)

Reference

Imports Statement (.NET Namespace and Type)

Extension methods can be defined only in modules

Add Imports Validation Error Dialog Box