?
Solved

Difference between a field and a property?

Posted on 2007-04-05
5
Medium Priority
?
1,382 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 1500 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

801 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