Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB.Net Properties - Public Get, Private Set

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.

like image 798
RiddlerDev Avatar asked Sep 22 '09 21:09

RiddlerDev


People also ask

What is get and set property in VB net?

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 .

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 does get private set mean?

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.

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 .


2 Answers

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 
like image 195
JDunkerley Avatar answered Oct 04 '22 22:10

JDunkerley


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

like image 33
Breeze Avatar answered Oct 04 '22 22:10

Breeze