“通过实例访问共享成员,常量成员,枚举成员或嵌套类型" [英] "Access of shared member, constant member, enum member or nested type through an instance"

查看:251
本文介绍了“通过实例访问共享成员,常量成员,枚举成员或嵌套类型"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I wonder why Visual Studio is raising this warning:

通过实例访问共享成员,常量成员,枚举成员或嵌套类型

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

通过实例"的含义是什么?另外,为什么这是警告"?

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

推荐答案

显示警告是一种设计选择.在C#中,使用实例(this)关键字调用静态变量时会抛出错误.

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.

更多有用的信息,请访问 MSDN .

More useful info at MSDN.

通过实例变量访问 Shared 成员可以掩盖成员是 Shared 的事实,从而使您的代码更难以理解.

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.

(...)

  • 使用定义共享成员的类或结构的名称来访问它,如以下示例所示.

  • 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

这篇关于“通过实例访问共享成员,常量成员,枚举成员或嵌套类型"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆