gpt4 book ai didi

.net - "Access of shared member, constant member, enum member or nested type through an instance"

转载 作者:行者123 更新时间:2023-12-04 02:05:10 27 4
gpt4 key购买 nike

不知道为什么 Visual Studio 发出此警告:

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



我的代码:
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

“通过实例”意味着什么?另外,为什么这是一个“警告”?

最佳答案

显示警告是一个设计选项。在 C# 中,使用实例 ( this ) 关键字调用静态时会引发错误。

问题是你应该调用对象来正确描述它是什么。

更多有用信息请访问 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

关于.net - "Access of shared member, constant member, enum member or nested type through an instance",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22377421/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com