Solved

Difference between a field and a property?

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





0
Comment
Question by:bobinorlando
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Clone Visual studio 2013 solution 5 24
Convert to Hour/minute textbox and compare 3 52
More on Time zones in vb 2010 12 37
VB.Net How to Exit Sub - Exit Form??? 5 47
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…
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

943 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now