Question: properties vs fields

  • Thread starter Thread starter VB Programmer
  • Start date Start date
V

VB Programmer

I was reading about xml serialization and the article referred to "public
properties and fields of an object". What is the difference between a
property and field? Can you provide an example?

Thanks.
 
Sure. The following VB.Net code defines a private Field that is exposed via
a public Property:

Private _Title As String = "This is a Field"
Public Property Title() As String
Get
If _Title = "This is a Field" Then
Return "This is a Property"
Else
Return _Title
End If
End Get
Set(ByVal Value As String)
_Title = Value
End Set
End Property

I added a few extras in the definition of the Property. The important thing
to remember is that a Property has a Get and a Set Method. Since these are
Methods, you don't have to necessarily link a Property to a Field, although
typically, Public Properties expose Private Fields. A Field is similar to a
variable, except that it is global to a class, and has Accessibility
(Public, Private, Protected, etc), whereas a variable has Function/Sub
scope, and is always invisible from outside of a class. In addition,
Properties can be Read Only or Write Only, as in the following example:

Private _Title As String = "This is a Field or Property, depending"
Public ReadOnly Property Title() As String
Get
Return _Title
End Get
End Property

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
Like many terms in our industry, the meaning can be context-specific (e.g.,
just ask someone what a 'domain' is without giving them any context and see
how many different responses you get).

So, for your question - in the context of an object, a field is typically a
Private variable that you define within the class that defines the object. A
public property provides access to the value of the variable. Note that a
property is really a method (it just has special syntax in VB/VB.NET). The
purpose of the property/method is to provide access to the value of a
private variable defined within the class.

Please let me know if you would like further clarification.

HTH.
 
A property is a set of accessors that give you access to some data. It
allows you to performs some additional calculates.

For example, suppose that you had an object that has a DiscountPercent
property where a sales person might want to set some purchase discount. You
might want to include validation in the property to ensure the value entered
is only between 0 and 100.

Properties also do not have to be directly related to some single values.
You might have a property called Subtotal, which would be calculated. You
would not be able to change the value since the subtotal is often
calculated.

To contrast, fields are only variables, no different then a variable you
define within your method, except that they are at the class level (global
to the class).

Although it is often to have variables that mean specific things that do not
require code logic, I try never to have a public field and have all access
go through properties, regardless of how simple the value is.
 
Thanks EVERYONE!

Peter Rilling said:
A property is a set of accessors that give you access to some data. It
allows you to performs some additional calculates.

For example, suppose that you had an object that has a DiscountPercent
property where a sales person might want to set some purchase discount. You
might want to include validation in the property to ensure the value entered
is only between 0 and 100.

Properties also do not have to be directly related to some single values.
You might have a property called Subtotal, which would be calculated. You
would not be able to change the value since the subtotal is often
calculated.

To contrast, fields are only variables, no different then a variable you
define within your method, except that they are at the class level (global
to the class).

Although it is often to have variables that mean specific things that do not
require code logic, I try never to have a public field and have all access
go through properties, regardless of how simple the value is.
 
Back
Top