Reputation: 3665
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?
Upvotes: 5
Views: 3241
Reputation: 12794
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.
Upvotes: 1
Reputation: 2685
Legacy Tutorials before this was a feature. The auto-implemented properties notation gives you a terse way of accomplishing consistency of access to your class
Upvotes: 0
Reputation: 545588
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.
Upvotes: 6