vb.net “通过实例访问共享成员、常量成员、枚举成员或嵌套类型”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22377421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"Access of shared member, constant member, enum member or nested type through an instance"
提问by Petr Ková?
I wonder whyVisual Studio is raising this warning:
我想知道为什么Visual Studio 会发出这个警告:
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"?
“通过实例”意味着什么?另外,为什么这是一个“警告”?
回答by Zanon
Showing a warning is a design option. In C#, it would throw an error when calling a static using an instance (this) keyword.
显示警告是一个设计选项。在 C# 中,使用 instance ( this) 关键字调用静态时会引发错误。
The problem is that you should call the object to correctly describe what it is.
问题是你应该调用对象来正确描述它是什么。
More useful info at MSDN.
更多有用的信息在MSDN。
Accessing a Sharedmember 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
通过实例变量访问Shared成员会掩盖该成员是Shared的事实,从而使您的代码更难以理解。
(...)
纠正这个错误
使用定义 Shared 成员的类或结构的名称来访问它,如以下示例所示。
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

