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
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.
Auto-implemented properties enable you to quickly specify a property of a class without having to write code to Get and Set the property.
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.
Read-only means that we can access the value of a property but we can't assign any value to it.
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.
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).
No, it isn't possible. You will have to create an explicit backing field.
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