VB.NET: Property with public getter and protected setter

18,822

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
Share:
18,822
jor
Author by

jor

Updated on June 02, 2022

Comments

  • jor
    jor almost 2 years

    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.