This is first way:
Public Class MyClass
Private _Property As String
Property Property () As String
Get
Return _Property
End Get
Set(ByVal Value As String)
_Property = Value
End Set
End Property
Public Function DoSomething() As String
Return _Property & Now()
End Function
End Class
Also I can do it in this way:
Public Class MyClass
Public Property As String
Public Function DoSomething() As String
Return Property & Now()
End Function
End Class
Which way is preferred?
The shorter way is preferred (by me) because it’s easier to read and understand. You can always switch to a fully written property definition if you need to do something special.
Some remarks though.
You should always use the property getter (Property) inside your code. Never use the internal variable (_Property). This is important because this way all interaction with that internal variable goes through a single point which you can than easily modify. Perhaps you want to format the property or throw an exception when the value is incorrect, etc. So the DoSomething function should be exactly the same in both methods.
Finally the Property shorthand is incorrect. It should be:
You didn’t give it a name. I assume you want to name it ‘Property’. I assume this is a reserved word and thus you probably would run into issues. By declaring it like this the compiler won’t trip over it. But you’d be wise to avoid reserved words in your class & method names.