VB.NET:具有公共getter和受保护setter的属性

时间:2021-06-15 21:19:58

In VB.NET is there a way to define a different scope for the getter and the setter of a property?

在VB.NET中有一种方法可以为getter和属性的setter定义不同的范围吗?

Something like (this code doesn't work of course):

类似的东西(这个代码当然不起作用):

Public Class MyClass
    Private mMyVar As String
    Public ReadOnly Property MyVar As String
        Get
            Return mMyVar
        End Get
    End Property
    Protected WriteOnly Property MyVar As String
        Set(value As String)
            mMyVar = value
        End Set
    End Property
End Class

I know that I could just accomplish this with a method that takes the property values as a parameter and sets the private variable. But I'm just curious whether there is a more elegant way that keeps closer to the concept of properties.

我知道我可以通过一个方法来实现这一点,该方法将属性值作为参数并设置私有变量。但我只是好奇是否有更优雅的方式更贴近属性的概念。

1 个解决方案

#1


17  

Sure, the syntax is as follows:

当然,语法如下:

Public Property MyVar As String
    Get
        Return mMyVar
    End Get
    Protected Set(value As String)
        mMyVar = value
    End Set
End Property

#1


17  

Sure, the syntax is as follows:

当然,语法如下:

Public Property MyVar As String
    Get
        Return mMyVar
    End Get
    Protected Set(value As String)
        mMyVar = value
    End Set
End Property