"Access of shared member, constant member, enum member or nested type through an instance"
Asked Answered
F

2

18

I wonder why Visual Studio is raising this warning:

Access of shared member, constant member, enum member or nested type through an instance

My code:

Dim a As ApplicationDeployment = deployment.Application.ApplicationDeployment.CurrentDeployment

If System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed Then
    If a.IsNetworkDeployed Then
        ' do something   
    End If
End If

What implies "through an instance"? Also, why is this a "warning"?

Filiano answered 13/3, 2014 at 11:37 Comment(3)
Your question is not that clear - which lines are you referring to a "first" and "second" in your update?Sexuality
The most simple reason for that warning is that you are using an instance variable (a in your example) to access a Shared method or property. VB allows this but generates a warning. C# does not allow it and it would be an error in C#.Bosomy
Which line shows the warning, and which one does not? It is not clear from your update.Bosomy
G
20

Showing a warning is a design option. In C#, it would throw an error when calling a static using an instance (this) keyword.

The problem is that you should call the object to correctly describe what it is.

More useful info at MSDN.

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.

(...)

To correct this error

  • 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()
            MsgBox("Hello")
        End Sub
    End Class
    
    Module testModule
        Public Sub Main()
            ' Access a shared method through an instance variable.
            ' This generates a warning.
            Dim tc As New testClass
            tc.sayHello()
    
            ' Access a shared method by using the class name.
            ' This does not generate a warning.
            testClass.sayHello()
        End Sub
    End Module
    
Group answered 30/9, 2016 at 1:16 Comment(0)
R
0

Zanor's answer is correct, but I'm not sure whether the OP fixed the code from it. This would be fixed like this:

If System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed Then
    If deployment.Application.ApplicationDeployment.CurrentDeployment.IsNetworkDeployed Then
        ' do something   
    End If
End If

IsNetworkDeployed is a Shared instance, which is what the message is complaining about. If you don't instantiate an object to reference it, then the warning will go away.

Yes, this is nearly four years old, but I just ran across something similar in my code, and was concerned about this additional part of the error message:

qualifying expression will not be evaluated.

It seems to be lying; my shared instance performs a calculation, and parameters passed are indeed used in the calculation, whether I use the shared function in an instance or without.

Reify answered 15/6, 2023 at 12:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.