Reputation: 83
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
Upvotes: 7
Views: 4349
Reputation: 172210
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.
Upvotes: 12
Reputation: 54999
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).
Upvotes: 5
Reputation:
thinkthing,
you may create a code snippet to add a generic property.
http://msdn.microsoft.com/en-us/library/ms165392(v=vs.90).aspx
Be mindful that visual studio has changed the basic way we build properties, with the get set, and now only one line is required with the get set understood. The full getter and setter can be built if you do need logic built within. I refer you here: http://msdn.microsoft.com/en-us/library/dd293589.aspx
and here, to a SO discussion regarding a similar discussion: Using snippets to make Class properties in VB.net. prop only gives "property () as " Whats up?
Upvotes: 0
Reputation: 234374
No, it isn't possible. You will have to create an explicit backing field.
Upvotes: 2