Is there any way to shorten the property declaration?

Asked

Viewed 53 times

3

I know in C# it is possible to do this:

public string propriedade { get; set; }

Is there any short way to declare properties with the procedures Get and Set in Visual Basic? The only way I know 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 writing Property and squeezing Tab, Visual Studio makes it easy to autocomplete with the name chosen for the property.

1 answer

3


The syntax should use the keyword Property:

Public Property Propriedade As String

That does the same as the question.

It can be initialized, equal to C#:

Public Property Propriedade As String = "Default"

You can also use a ReadOnly, but you’d have to write the reading code, so it’s not as practical as in C#:

Public ReadOnly Property Propriedade As String = "Default"

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

Public WriteOnly Property Propriedade As String

I put in the Github for future reference.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.