ReadOnly Property or property with private set I should use in vb.net?
Solution 1
In the case of ReadOnly
, only those with access to the underlying variable may change the underlying value (i.e. elements within the same class, for instance) by directly applying such a change. In the latter case, Private Set
- this is much the same - elements within the scope of the class can change the underlying value, but can do so by means of the property.
Which one is preferred is circumstantial: one advantage of properties is that you can, like a method, have further implementation involved when applying the change (although side-effects should be avoided, you might 'validate' and take exception, for instance). If there is always something else to do when setting the value, that is strongly related to setting the value, you might do it within this property setter, as opposed to having to code that implementation everywhere you do the set
.
Solution 2
Note that if you are using the Roslyn compilers (.NET 4.6 and higher, VS.NET 2015+), then even when the short VB.NET form is used
Public ReadOnly Property Name as String
with no private variable, constructors for the class are still allowed to assign values to the readonly property. You can even pass the property to other functions as a ByRef
parameter.
Public Class SomeClass
Public ReadOnly Property Name1 As String
Public ReadOnly Property Name2 As String
Public Sub New()
PrivSub(Name1)
Name2 = Name1 & " is now"
End Sub
Private Sub PrivSub(ByRef n As String)
n = System.DateTime.UtcNow.ToLongDateString()
End Sub
End Class
DotNetFiddle of this class
Solution 3
The first property declaration ReadOnly
makes it so the property cannot be modified at all. The second Private Set
allows the property to be modified within the class you are working in Me.Name = "str"
.
In both cases the underlying value can still be changed within the class using _Name = "str"
.
Solution 4
the first block will only allow you to get the value of Name. you cannot set Name.
the second block allows you to set the value of Name from within the class. example:
Me.Name = "new value"
I would choose option 1 because the second option verbose without providing any real value.
Amir Ismail
I want to learn Blog +Amir Ismail LinkedIn GitHub
Updated on September 30, 2020Comments
-
Amir Ismail over 3 years
I Like .NET automatic properties, in C# it so easy to declare
readonly
property by declaring itsset
section asprivate
like this:public String Name{ get; private set; }
But when I tried that in VB.NET I was shocked that it is not supported as mentioned here and I have to write it as follows:
Private _Name as String Public ReadOnly Property Name as String Get return _Name End Get End Property
Or:
Private _Name as String Public Property Name as String Get return _Name End Get Private Set(value as String) _Name = value End Set End Property
What the difference between these declarations in
VB.NET
, which one is preferred and Why?Edit
Which one will affect compile time, runtime or performance at all?