Access of shared member through an instance; qualifying expression will not be evaluated

An instance variable of a class or structure is used to access a Shared variable, property, procedure, or event defined in that class or structure. This warning can also occur if an instance variable is used to access an implicitly shared member of a class or structure, such as a constant or enumeration, or a nested class or structure.

The purpose of sharing a member is to create only a single copy of that member and make that single copy available to every instance of the class or structure in which it is declared. It is consistent with this purpose to access a Shared member through the name of its class or structure, rather than through a variable that holds an individual instance of that class or structure.

Accessing a Shared member through an instance variable can make your code more difficult to understand by obscuring the fact that the member is Shared. Furthermore, if such access is part of an expression that performs other actions, such as a Function procedure that returns an instance of the shared member, Visual Basic bypasses the expression and any other actions it would otherwise perform.

For more information and an example, see Shared (Visual Basic).

By default, this message is a warning. For more information about hiding warnings or treating warnings as errors, see Configuring Warnings in Visual Basic.

Error ID: BC42025

To correct this error

  • Use the name of the class or structure that defines the Shared member to access it.

  • Be alert for the effects of scope when two programming elements have the same name. The following example declares a variable testClass with the same name as the class to which it is typed. The compiler interprets the call to sayHello() as an access through the class name testClass, and no warning occurs.

    Public Class testClass
        Public Shared Sub sayHello()
            MsgBox("Hello")
        End Sub
    End Class
    Module testModule
        Public Sub Main()
            Dim testClass As testClass = Nothing
            testClass.sayHello()
        End Sub
    End Module
    

See Also

Concepts

Scope in Visual Basic

Reference

Shared (Visual Basic)