Click here to Skip to main content
15,885,985 members
Home / Discussions / C#
   

C#

 
QuestionDiff. between properties and fields? Pin
Brian Olej26-Oct-02 9:46
Brian Olej26-Oct-02 9:46 
AnswerRe: Diff. between properties and fields? Pin
Daniel Turini26-Oct-02 9:58
Daniel Turini26-Oct-02 9:58 
GeneralRe: Diff. between properties and fields? Pin
Brian Olej26-Oct-02 10:02
Brian Olej26-Oct-02 10:02 
GeneralRe: Diff. between properties and fields? Pin
Anonymous26-Oct-02 10:06
Anonymous26-Oct-02 10:06 
GeneralRe: Diff. between properties and fields? Pin
Brian Olej26-Oct-02 10:26
Brian Olej26-Oct-02 10:26 
GeneralRe: Diff. between properties and fields? Pin
leppie26-Oct-02 10:14
leppie26-Oct-02 10:14 
GeneralRe: Diff. between properties and fields? Pin
Daniel Turini26-Oct-02 10:18
Daniel Turini26-Oct-02 10:18 
GeneralRe: Diff. between properties and fields? Pin
Jon Rista26-Oct-02 13:50
Jon Rista26-Oct-02 13:50 
Yeah, its best to use properties rather than public fields. A property acts as an encapsulation around and a buffer between a classes private fields and the function accessing them.

Where with a public field all you can do is get and set:

class Test
{
public int ABool = 0;
}

Test t = new Test();
t.ABool = 1;


A property allows you to do many things before the field itself is set. A property also allows you to restrict the access to a field, allowing only getting or setting, if that is what your class requires:

class Test
{
private int aBool = 0;

public int ABool
{
set
{
// restrict the value of aBool to positive and zero
if (value >= 0)
{
aBool = value;
}
}
get
{
return aBool;
}
}
}

Test t = new Test();
t.ABool = -1;
GeneralRe: Diff. between properties and fields? Pin
Brian Olej29-Oct-02 8:45
Brian Olej29-Oct-02 8:45 
GeneralRe: Diff. between properties and fields? Pin
Brian Olej26-Oct-02 10:25
Brian Olej26-Oct-02 10:25 
GeneralRe: Diff. between properties and fields? Pin
Kevin McFarlane27-Oct-02 4:56
Kevin McFarlane27-Oct-02 4:56 
GeneralRe: Diff. between properties and fields? Pin
leppie27-Oct-02 5:00
leppie27-Oct-02 5:00 
AnswerRe: Diff. between properties and fields? Pin
LongRange.Shooter28-Oct-02 10:46
LongRange.Shooter28-Oct-02 10:46 
GeneralRe: Diff. between properties and fields? Pin
Brian Olej29-Oct-02 8:46
Brian Olej29-Oct-02 8:46 
GeneralRe: Diff. between properties and fields? Pin
Senkwe Chanda31-Oct-02 2:22
Senkwe Chanda31-Oct-02 2:22 
GeneralRe: Diff. between properties and fields? Pin
LongRange.Shooter31-Oct-02 9:54
LongRange.Shooter31-Oct-02 9:54 
GeneralNamed pipes Pin
EricK726-Oct-02 9:39
sussEricK726-Oct-02 9:39 
GeneralRe: Named pipes Pin
Daniel Turini26-Oct-02 9:50
Daniel Turini26-Oct-02 9:50 
GeneralRe: Named pipes Pin
EricK729-Oct-02 5:26
sussEricK729-Oct-02 5:26 
QuestionHow can I . . . Pin
Venet25-Oct-02 23:43
Venet25-Oct-02 23:43 
AnswerRe: How can I . . . Pin
Stephane Rodriguez.26-Oct-02 0:31
Stephane Rodriguez.26-Oct-02 0:31 
GeneralRe: How can I . . . Pin
Venet26-Oct-02 1:04
Venet26-Oct-02 1:04 
GeneralRe: How can I . . . Pin
Stephane Rodriguez.26-Oct-02 2:26
Stephane Rodriguez.26-Oct-02 2:26 
GeneralRe: How can I . . . Pin
Venet26-Oct-02 2:33
Venet26-Oct-02 2:33 
AnswerRe: How can I . . . Pin
ian mariano28-Oct-02 4:38
ian mariano28-Oct-02 4:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.