Public and Properties

I saw some applications using different class and properties..
does PUBLIC of any variable can be equal to a public property
like the following 2 versions of examples will result the same thing if we call them or
inherits.

I need more explainations between these two class and usage of properties and public varibles.

(the code is not as is.. just a rough draft)

=== version 1 ===
Class Car
  public Doors as integer
  public Windows as integer
  public Tires as integer

  public sub Accelerate()
  ......
   (more code)
  ......
end class

=== version 2 ===
Class Car
  public Property Doors() as integer
  public Property Windows() as integer
  public Property Tires() as integer

  public sub Accelerate()
  ......
   (more code)
  ......
end class



Answer this question

Public and Properties

  • JHalmans

    Another reason to use Properties and NOT Fields is that by using Fields you are locked into a "contract" with any object that uses your Fields, whereas if you use Properties, you are not locked into a "contract" with any consuming classes, meaning you could change the underlying logic of what values get returned or how the values get set to an underlying Field in your Property without changing any code in the consuming class, whereas with Fields you would have to make changes on both ends.
  • Bogdan.B

    Well, the Public variables are called Fields.  There isn't a whole lot of difference between using a Public Field or a Public Property.  However, it's generally a good idea to use a property if, perhaps in the future, you need to perform validation when being set or something.  With a property, you can do that in the Business Layer, where with a field you will need to do that where the value gets set...  I like properties better simply b/c I can have everything I need in one class.
  • Public and Properties