BC42025: Access of shared member, constant member, enum member or nested type 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.

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

Example

The following example generates bc42025:

Public Class TestClass
    Public Shared Sub SayHello()
        Console.WriteLine("Hello")
    End Sub
End Class

Module Program
    Public Sub Main()
        Dim tc As New TestClass()
        tc.SayHello() ' BC42025.
    End Sub
End Module

To address this warning

Use the name of the class or structure that defines the Shared member to access it, as shown in the following example:

Public Class TestClass
    Public Shared Sub SayHello()
        Console.WriteLine("Hello")
    End Sub
End Class

Module Program
    Public Sub Main()
        TestClass.SayHello()
    End Sub
End Module

Note

Be alert for the effects of scope when two programming elements have the same name. In the previous example, if you declare an instance by using Dim testClass As TestClass = Nothing, the compiler treats a call to testClass.SayHello() as an access of the method through the class name, and no warning occurs.

See also