Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB.net automatic property - readonly?


is it possible (and how) to make a readonly automatic property in VB 2010?

Public Class Foo  
  Public Property Value As Integer  
  Public Sub New()  
      _Value = 123
  End Sub  
End Class  

problem is that users can write to the property. thanx

like image 725
thinkthing Avatar asked Jan 31 '11 14:01

thinkthing


People also ask

How do I make a property read-only in VB net?

Read-only fields in visual basic can be created by using ReadOnly keyword. In visual basic, the read-only fields can be initialized either at the declaration or in a constructor. The read-only field values will be evaluated during the run time in visual basic.

What is auto implemented property?

Auto-implemented properties enable you to quickly specify a property of a class without having to write code to Get and Set the property.

What are properties in VB net?

A property is a value or characteristic held by a Visual Basic object, such as Caption or Fore Color. Properties can be set at design time by using the Properties window or at run time by using statements in the program code. Object. Property = Value.

What is read-only property in IOS?

Read-only means that we can access the value of a property but we can't assign any value to it.


3 Answers

It is now supported in VB14 (Visual Studio 2015 and later):

Public Class Foo  
    Public ReadOnly Property Value As Integer = 123
End Class  

See https://github.com/dotnet/roslyn/wiki/New-Language-Features-in-VB-14#read-only-auto-properties

In earlier versions, you need to create a backing field.

like image 133
Heinzi Avatar answered Oct 03 '22 18:10

Heinzi


No, VB.Net does not support readonly auto properties. See this MS Connect issue for the reasoning behind this (specifically the comment made by Jonathan Aneja).

like image 35
Hans Olsson Avatar answered Oct 03 '22 19:10

Hans Olsson


No, it isn't possible. You will have to create an explicit backing field.

like image 29
R. Martinho Fernandes Avatar answered Oct 03 '22 18:10

R. Martinho Fernandes