.net - 访问 VB.NET 中 protected 成员

标签 .net vb.net

根据这个post下面的代码应该可以编译,但不能编译。

class Base
    protected m_x as integer
end class

class Derived1
    inherits Base
    public sub Foo(other as Base)
        other.m_x = 2
    end sub
end class

class Derived2
    inherits Base
end class

这可能是什么问题?我刚刚创建了一个新的 VB.NET 控制台项目并复制粘贴了代码。

我收到的错误消息是:“SampleProject.Base.m_x”在此上下文中无法访问,因为它是“ protected ”,并且我已经检查了不同的 .NET 框架版本(2.0、3.0 和 3.5)。

最佳答案

protected 成员只能通过MyBase.m_x(C# 中的基类)从派生类访问。 你可以这样写:

public sub Foo(other as Base)
    MyBase.m_x = 2
end sub

之所以other.m_x = 2无法编译是因为other不是(或者不一定是)当前实例的基类实例派生1。它可以是 Base 的任何实例,因为它是一个参数值。

关于.net - 访问 VB.NET 中 protected 成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4304442/

相关文章:

.net - 在 Visual Studio 2010 中,为什么要创建 .NETFramework,Version=v4.0.AssemblyAttributes.cpp 文件,我可以禁用它吗?

.net - VB.Net 为什么 Math.Round 将 5 舍入到最接近的偶数,我该怎么办?

.net - 如何在64位.NET应用程序中显示SVG文件?

vb.net - VB.NET 中的“List(Of Byte() )' to ' Byte()”

vb.net - 在VB.Net中动态添加图片框

SQL 查询表别名

c# - 您可以将数据绑定(bind)到包含 Silverlight 参数的属性吗?

c# - 可空类型存储在内存中的什么位置?

c# - 如何控制下拉列表中显示的屏幕保护程序(友好/显示)名称? - Windows Server 2003/Windows 2000

asp.net - 如何在转发器中找到一个 div/panel 并应用 css?