'VB.NET: Property with public getter and protected setter

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.



Solution 1:[1]

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

Solution 2:[2]

Hope this is more helpful as accepted answer requires few correction -

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Heinzi
Solution 2 raghav-wd