?
Solved

Field vs Property?

Posted on 2006-03-31
9
Medium Priority
?
256 Views
Last Modified: 2010-04-16
Hi
whats the difference between a Field and a Property?

If I can modify a Field through a method then isn't that just the same thing?

Thanks
Paul
0
Comment
Question by:paulwhelan
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 80 total points
ID: 16342121
A field technically refers to a variable stored inside an object. Rather than simply making the field public you usually use properties to give you more control over how your fields are accessed. For example, using a property you can make a field read or write only, you can also perform validation when someone tries to set the value, or the value returned by the property could be calculated at runtime.

Plus if you decide to change the type of a field, or how it is stored, you can do so without breaking anything that consumes your object.
0
 
LVL 14

Expert Comment

by:existenz2
ID: 16347003
In short:

public string test = ""; // Field

public string Test { // Property
 get {
  return test;
}
set {
  test = value;
}
}

The advantage from using properties are that you:
A) can define if people are allowed to get/set data (both are optional)
B) can do validation before you set/get the data
C) can perform custom things before data is get/set
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Expert Comment

by:woigl
ID: 16347130
To the example before i would suggest to use the field private, otherwise you give full access outside the class...

private string test = ""; // Field

public string Test { // Property
get {
  return test;
}
set {
  test = value;
}
}

Even internal is the handling of the fields and properties different.

this kind of get and set functions are much better handled compare to own written functions to control get and set.
0
 
LVL 14

Expert Comment

by:existenz2
ID: 16347196
I agree with woigl that in cases when you want to have fields available to outside the class you should make use of properties instead of public fields.
0
 

Expert Comment

by:woigl
ID: 16353784
One more reason for properties is the use of Interfaces.

A Interface can not contain fields, but it can contain Properties.
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 16354236
Also, while we're covering all bases, you need to use properties if you want to support databinding, as WinForms controls can only bind to properties and not directly to fields.
0
 

Author Comment

by:paulwhelan
ID: 16361103
Thanks guys!

So I need to use both properties and fields?

Cheers
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 16361120
Yes. Fields are the (usually) data storage that are accessed by the properties.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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 This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month15 days, 4 hours left to enroll

840 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