15
votes

In VB.NET is there a way to define a different scope for the getter and the setter of a property?

Something like (this code doesn't work of course):

Public Class MyClass
    Private mMyVar As String
    Public ReadOnly Property MyVar As String
        Get
            Return mMyVar
        End Get
    End Property
    Protected WriteOnly Property MyVar As String
        Set(value As String)
            mMyVar = value
        End Set
    End Property
End Class

I know that I could just accomplish this with a method that takes the property values as a parameter and sets the private variable. But I'm just curious whether there is a more elegant way that keeps closer to the concept of properties.

1

1 Answers

20
votes

Sure, the syntax is as follows:

Public Property MyVar As String
    Get
        Return mMyVar
    End Get
    Protected Set(value As String)
        mMyVar = value
    End Set
End Property