Auto Real estate with an open getter and a private setter

NOTE. Is this not a duplicate of the equivalent of VB.NET C # string reduction? , This question is about how to have different permissions on the getter and setter for the automatic VB property; for example, a public getter and a private setter. This question is about the auto-property syntax (and does not mention this problem).


I am trying to convert the auto property ( public getter and private setter) from C # to VB.NET.

But after conversion, VB.NET maintains a private field.

C # code

class DemoViewModel { DemoViewModel (){ AddCommand = new RelayCommand(); } public ICommand AddCommand {get;private set;} } 

Equivalent to VB.NET from Code Converter

 Class DemoViewModel Private Sub New() AddCommand = New RelayCommand() End Sub Public Property AddCommand() As ICommand Get Return m_AddCommand End Get Private Set m_AddCommand = Value End Set End Property Private m_AddCommand As ICommand End Class 

VB.NET code creates a private support field.

Is it possible to get rid of this back field in the source code (for example, C #)? How?

Without this function, the VB.NET source will have such redundancy.

+9
source share
3 answers

Using VB.NET, if you want to indicate different accessibility for the Get and Set procedure, you cannot use an automatically implemented property and instead use the standard, or extended property syntax.

Read MSDN: http://msdn.microsoft.com/en-us/library/dd293589.aspx


If getter and setter have the same availability , for example. both values ​​are Public , then you can use the syntax of the automatic property , for example:

 Public Property Prop2 As String = "Empty" 
+13
source

as the answer is above (s), you can enter Public Prospectus to expose Private. This may not be a pleasant solution, but even less code than the extended property syntax

 Private Property internalprop as object Public Readonly Property exposedprop as Object = internalprop 
0
source

In VB.NET it is

 Public ReadOnly Property Value As String 

You then use the underscore in front of your property name to access a private network device.

 Me._Value = "Fred" 
0
source

All Articles