Solved

Difference between a field and a property?

Posted on 2007-04-05
5
1,380 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

707 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