whytheq
whytheq

Reputation: 35605

Why can't I have auto-implemented Read-only properties

This is allowed:

Public Property Text() As String

Whereas for a read-only property why aren't I allowed an equivalent?

Public ReadOnly Property Text() As String

I seem to be forced to use:

Public ReadOnly Property Text() As String
    Get
        Return fText
    End Get
End Property

Upvotes: 4

Views: 2100

Answers (1)

Heinzi
Heinzi

Reputation: 172478

It is now supported in VB14 (Visual Studio 2015 and later). Auto-implemented properties can be initialized with initialization expressions:

Public ReadOnly Property Text1 As String = "SomeText"
Public ReadOnly Property Text2 As String = InitializeMyText()

or in the constructor:

Public ReadOnly Property Text As String

Public Sub New(text As String)
    Me.Text = text
End Sub

Details:

Upvotes: 5

Related Questions