I figured I would ask... but is there a way to have the Get part of a property available as public, but keep the set as private?
Otherwise I am thinking I need two properties or a property and a method, just figured this would be cleaner.
In this articleThe Get procedure retrieves the property's value, and the Set procedure stores a value. If you want the property to have read/write access, you must define both procedures. For a read-only property, you define only Get , and for a write-only property, you define only Set .
Assigning a Value. Code consuming a ReadOnly property cannot set its value. But code that has access to the underlying storage can assign or change the value at any time. You can assign a value to a ReadOnly variable only in its declaration or in the constructor of a class or structure in which it is defined.
The public setter means that the value is editable by any object present outside the class. On the other hand, the private setter means that the property is read-only and can't be modified by others.
The Property statement can declare the data type of the value it returns. You can specify any data type or the name of an enumeration, structure, class, or interface. If you do not specify returntype , the property returns Object .
Yes, quite straight forward:
Private _name As String Public Property Name() As String Get Return _name End Get Private Set(ByVal value As String) _name = value End Set End Property
I'm not sure what the minimum required version of Visual Studio is, but in VS2015 you can use
Public ReadOnly Property Name As String
It is read-only for public access but can be privately modified using _Name
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With