In many of the articles I have read on the web say that when creating properties in vb.net, they should use the get/set methods and a private/protected member variable in the class.
Like so:
Public Class Person
Private _name as string
public property Name as string
get
return _name
end get
set(byval value as string)
_name = value
end set
end property
end class
If there is no logic in the get/set of the property, why wouldn't one write that same property like this:
Public class Person
Public Property Name as string
end class
Is this because properties were intended to just be accessors into the class from outside and you would store the variable in the class?
The reason is that these guidelines and tutorials were published before VB.NET 4.0 came out. There’s no other reason not to use automatically implemented properties.
While Konrad has it spot on, I'll add that being a tutorial, educating the student on how properties work is more important than shortcut implementation. A more modern tutorial should show the expanded code, then the shortcut.
Ultimately, this depends on the tutorial, whether it's about programming fundamentals and methodology, or about a specific feature.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With