共用方式為


How to: Qualify a Declared Element Name 

When your code refers to a declared element, the Visual Basic compiler must match the name in your reference to the appropriate declaration of that name. If the element is defined outside your code, or if more than one element is declared with the same name, you might need to qualify the name to allow the compiler to resolve any possible ambiguity or to locate the element.

Qualifying a name means preceding it in your source statement with information that identifies where the target element is defined. This information is called a qualification string. It can include one or more namespaces and a module, class, or structure.

The qualification string should unambiguously specify the module, class, or structure containing the target element. The container might in turn be located in another containing element, usually a namespace. You might need to include several containing elements in the qualification string.

To access a declared element by qualifying its name

  1. Determine the location in which the element has been defined. This might include a namespace, or even a hierarchy of namespaces. Within the lowest-level namespace, the element must be contained in a module, class, or structure.

    ' Assume the following hierarchy exists outside your code.
    Namespace outerSpace
        Namespace innerSpace
            Module holdsTotals
                Public Structure totals
                    Public thisTotal As Integer
                    Public Shared grandTotal As Long
                End Structure
            End Module
        End Namespace
    End Namespace
    
  2. Determine a qualification path based on the target element's location. Start with the highest-level namespace, proceed to the lowest-level namespace, and end with the module, class, or structure containing the target element. Each element in the path must contain the element that follows it.

    outerSpaceinnerSpaceholdsTotalstotals

  3. Prepare the qualification string for the target element. Place a period (.) after every element in the path. Your application must have access to every element in your qualification string.

    outerSpace.innerSpace.holdsTotals.totals.
    
  4. Write the expression or assignment statement referring to the target element in the normal way.

    grandTotal = 9000
    
  5. Precede the target element name with the qualification string. The name should immediately follow the period (.) that follows the module, class, or structure that contains the element.

    ' Assume the following module is part of your code.
    Module accessGrandTotal
        Public Sub setGrandTotal()
            outerSpace.innerSpace.holdsTotals.totals.grandTotal = 9000
        End Sub
    End Module
    
  6. The compiler uses the qualification string to find a clear, unambiguous declaration to which it can match the target element reference.

See Also

Tasks

How to: Distinguish Between Two Elements with the Same Name
How to: Hide a Variable with the Same Name as Your Variable
How to: Hide an Inherited Variable
How to: Access a Variable Hidden by a Derived Class

Concepts

Declared Element Characteristics
Resolving a Reference When Multiple Variables Have the Same Name
Declaration Statements in Visual Basic

Other Resources

References to Declared Elements