Export (0) Print
Expand All

Add Imports Validation Error Dialog Box

The Add Imports Validation Error dialog box appears when the compiler determines that adding an Imports statement to define an unrecognized type name will introduce other changes in the meaning of your code. 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 it is defined 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.

The Add Imports Validation Error dialog box presents the following options for fixing the problem.

Import namespace, and qualify the affected identifiers

Inserts an Imports namespace statement into the file. Each type name that is undefined in the file but defined in namespace will be bound to its definition in the imported namespace. This includes all occurrences of the type name that caused the error.

Each type name that is already defined in the file, and that this Imports statement would change, is prefixed with the necessary namespace, module, or class name to retain its original meaning.

Do not import namespace, but change typeName 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 rest of the file is not affected.

Cancel

Closes the dialog box.

Within 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 ConsoleApplication1.Ns2

Module Module1

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

End Module

' Namespace Ns1 defines two classes, C1 and C2.
Namespace Ns1

    Class C1
    End Class

    Class C2
    End Class

End Namespace

' Namespace Ns2 defines one class, C1.
Namespace Ns2

    Class C1
    End Class

End Namespace

Rest the mouse pointer over C2 in Main and click the Error Correction Options exclamation point. You will see two options for fixing the problem:

  • Import ConsoleApplication1.Ns1

  • Change 'C2' to 'Ns1.C2'

Error Correction Options

Shows Error Correction options.

If you choose the second option, C2 is changed to Ns1.C2 in your code and the error is resolved. Only the current instance of C2 is changed.

If you choose the first option, the situation is more complicated in this example. The compiler detects that adding the Imports statement would cause other changes in the meaning of your code, and the Add Imports Validation Error dialog box appears. Specifically, importing ConsoleApplication.Ns1 to fix the problem with C2 causes a new error for C1 in the first line of Main: Dim instance1 As New C1. Two definitions of class C1 are now in scope: the one defined in namespace Ns1 and the one defined in namespace Ns2.

The Add Imports Validation Error dialog box gives you two choices:

  • Import 'ConsoleApplication1.Ns1' and qualify the affected identifiers.

  • Do not import 'ConsoleApplication1.Ns1', but change 'C2' to 'Ns1.C2'.

Add Imports Validation Error dialog box

Shows choices in the dialog box.

If you choose the first option, Imports ConsoleApplication1.Ns1 is added at the top of the file, and Dim instance1 As New C1 is changed to Dim instance1 As New ConsoleApplication1.Ns2.C1 to preserve the original binding of C1.

NoteNote:

This is a good solution when the undefined type name occurs many times in the file, or when other elements from the namespace are likely to be useful.

If you choose the second option, C2 is changed to Ns1.C2 in your code and the error is resolved. Only the current instance of C2 is changed.

NoteNote:

This limited solution is recommended when the undefined type name occurs only one or two times in the file, or when the namespace that the type name is defined in is large.

Community Additions

ADD
Show:
© 2014 Microsoft