18

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.

2 Answers 2

22

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 
Sign up to request clarification or add additional context in comments.

Comments

0

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 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.