Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReadOnly Property or property with private set I should use in vb.net?

I Like .NET automatic properties, in C# it so easy to declare readonly property by declaring its set section as private 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?

like image 984
Amir Ismail Avatar asked Jan 26 '12 16:01

Amir Ismail


People also ask

How do I set the ReadOnly property in VB net?

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.

What is ReadOnly in VB net?

In VB.NET the read-only property is usually created to be read-only from external class. If you want to set this property, you can easily do it from inside the class, by changing the realated local variable. So, e.g. in VB 2010 Public ReadOnly Property SomeVariable() As String.

Can ReadOnly property be set in constructor?

You can initialize a ReadOnly property in the constructor or during object construction, but not after the object is constructed.

What is the use of property in VB net?

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 .


1 Answers

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.

like image 107
Grant Thomas Avatar answered Nov 15 '22 15:11

Grant Thomas