VB.NET:具有公共 getter 和受保护 setter 的属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17570863/
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
VB.NET: Property with public getter and protected setter
提问by jor
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.
我知道我可以使用将属性值作为参数并设置私有变量的方法来完成此操作。但我只是好奇是否有更优雅的方式来更接近属性的概念。
回答by Heinzi
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

