Does VB.NET have an equivalent of C# auto property with custom access specifier?
I was simply wondering if in VB.Net there is a shorthand equivalent to this kind of C# including the private
setter:
pu开发者_运维百科blic string Test { get; private set; }
Can someone please tell me the shortest way to achieve this in VB.Net?
Sorry, this is not possible in VB.NET:
Auto-implemented properties are convenient and support many programming scenarios. However, there are situations in which you cannot use an auto-implemented property and must instead use standard, or expanded, property syntax.
You have to use expanded property-definition syntax if you want to do any one of the following:
- ...
- Create properties that are WriteOnly or ReadOnly.
- ...
Unfortunately, you can't use different accesibilities for auto property accessors in VB.NET. You have to manually write the property code.
Like this:
Private _test As String
Public Property Test() As String
Get
Return _test
End Get
Private Set(ByVal Value As String)
_test = Value
End Set
End Property
No alternative.
精彩评论