Solved

Difference between a field and a property?

Posted on 2007-04-05
5
1,379 Views
Last Modified: 2008-02-01
What is the difference between a field and a property in VB.net?





0
Comment
Question by:bobinorlando
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 35

Expert Comment

by:YZlat
ID: 18857976
can you specify what do you mean?
0
 
LVL 2

Accepted Solution

by:
Cazna earned 500 total points
ID: 18858717
A field is simply a variable in which you can store data.  A property on the other hand allows you to execute code when values are either read or written.  This can be usefull for processing user input or for enforcing buisness rules.  Also with a property you can specify it to be either ReadOnly or WriteOnly and prevent the other action from being performed.

Commonly you will use a combination of a private class field and a public property. Something like this:

Private _foo as String

Public Property Foo() As String
    Get
        Return _foo
        'Here you can put code that will execute each time the variable is accessed eg:
        accessCounter += 1
    End Get
    Protected Set(ByVal value As String)
        _foo = value
        'Here you can put some extra code to execute when the variable is set eg:
        isFooSet = True
    End Set
End Property
0
 
LVL 2

Expert Comment

by:Cazna
ID: 18858768
The reason that the field is private and the property is public is to force other classes to access the field through the property so that you know the code in your property is going to be run.
0
 
LVL 1

Author Comment

by:bobinorlando
ID: 18859623
Well,  is it true to say that all variables are fields?

I'm looking for what we used to call the salient anatomical features, or a definition of a field that would distinguish it from all other object types.

For example, can other things be fields (e.g. are properties ever referred to as fields, or do properties have fields, which objects have fields and which don't)?

Or does the use of field overlap and duplicate other vb.net terms for objects and types?
In other words, what are all of the various meanings (other than database meanings which I understand) that "field" is used in vb.net?

Thank you.





0
 
LVL 2

Expert Comment

by:Cazna
ID: 18862444
"Field" is a term more often used by programmers using "C" based languages but in VB.NET it has the same meaning.  In a class you have written it will generally only refer to variables although when accessing other classes say in a DLL some programmers will also refer to properties as fields although I do not find this practice to be accurate.  The origins of the term field was simply as a location to store data so technically a property is not a field as it is only a method of accessing data.
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question