Reputation: 113
I am working in vb.net and have a couple of variables in a class. While looking over my code I am trying to decide if I have declared my variables appropriately. I have two dates, two strings, a list, and a thread. I have the first four declared as private properties and the last two as just variables. What is the best practice for deciding which one use?
Upvotes: 2
Views: 267
Reputation: 43743
The value of having something as a property, as opposed to a field, is that you can customize when happens when the value is read or set (the getter and setter methods). If the value is public, it's typically best to always make it a property, since you may need to customize the behavior at some point in the future, and to change a public field to a public property will break compatibility with already compiled code. However, if the value is private, then that is not even a concern. Therefore, for simplicity sake, I would say that all private values should be stored as fields, unless you need to customize its behavior with property getter/setter methods.
Upvotes: 1
Reputation: 6375
You can read about the difference in this MSDN Article. http://msdn.microsoft.com/en-us/library/sk5e8eth.aspx
Basically if you only want to store a local value without anything else in your class you use a variable. Properties offer more advanced features. You can execute code to your liking if you assign a value to a property or retrieve it (if you define Get/Set methods for the property, again see the MSDN articles). If the value is public I would suggest a property since you can more tightly control what it is set to by using for example over/underflow checks in the Set method.
Upvotes: 1