Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vb.net get/set Properties without logic

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?

like image 467
Jim Avatar asked Jan 19 '12 14:01

Jim


2 Answers

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.

like image 107
Konrad Rudolph Avatar answered Nov 11 '22 18:11

Konrad Rudolph


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.

like image 30
Hand-E-Food Avatar answered Nov 11 '22 18:11

Hand-E-Food