Is there any way to shorten the property declaration?

1

I know that in C # you can do this:

public string propriedade { get; set; }

Is there any short way of declaring properties with the Get and Set procedures in Visual Basic? The only way I know it is this:

Private _propriedade As String
Public Property propriedade() As String
   Get
      Return _propriedade
   End Get
   Set(ByVal value As String)
       _propriedade = value
   End Set
End Property

I also know that by typing Property and pressing Tab , Visual Studio makes it easier to autocomplete with the chosen name for the property.

    
asked by anonymous 18.09.2016 / 23:12

1 answer

1

The syntax should use the keyword Property :

Public Property Propriedade As String

This does the same as the question.

It can be initialized, just like C #:

Public Property Propriedade As String = "Default"

You can also use ReadOnly , but you would have to write the reading code, so it is not as practical as in C #:

Public ReadOnly Property Propriedade As String = "Default"

Or WriteOnly (you also have to write the Set code):

Public WriteOnly Property Propriedade As String
    
18.09.2016 / 23:32