Solved

Difference between a field and a property?

Posted on 2007-04-05
5
1,373 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
Comment Utility
can you specify what do you mean?
0
 
LVL 2

Accepted Solution

by:
Cazna earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
"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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

771 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

10 Experts available now in Live!

Get 1:1 Help Now