Click here to Skip to main content
15,914,225 members
Home / Discussions / C#
   

C#

 
GeneralRe: DataGrid & Relations Pin
Heath Stewart25-Nov-03 3:35
protectorHeath Stewart25-Nov-03 3:35 
GeneralRe: DataGrid & Relations Pin
mgaerber25-Nov-03 20:34
mgaerber25-Nov-03 20:34 
GeneralRe: DataGrid & Relations Pin
Heath Stewart26-Nov-03 2:23
protectorHeath Stewart26-Nov-03 2:23 
GeneralRe: DataGrid & Relations Pin
Anonymous26-Nov-03 22:15
Anonymous26-Nov-03 22:15 
GeneralThe csc.exe compiler - member variables Pin
tom7624-Nov-03 23:54
tom7624-Nov-03 23:54 
GeneralRe: I've changed my code and it works - this is awful! Pin
tom7625-Nov-03 0:33
tom7625-Nov-03 0:33 
GeneralAh, Properties! Pin
tom7625-Nov-03 0:54
tom7625-Nov-03 0:54 
GeneralRe: Ah, Properties! Pin
Heath Stewart25-Nov-03 3:16
protectorHeath Stewart25-Nov-03 3:16 
Because properties allow you to perform validation and other useful functions, where fields cannot. Lets say your Person class has an Age property (in actuality, it's better to store their birthday, but this is only an example...). You could use a UInt16 or something, but unsigned integers are not CLS-compliant. So, you want to make sure that only a positive integer is passed to the Age property:
public class Person
{
  private int age;
  public int Age
  {
    get { return this.age; }
    set
    {
      if (value <= 0) throw new ArgumentException();
      this.age = value;
    }
}
This is used a lot throughout the .NET Framework and in many other applications, even in non-.NET applications (like in VB, Java Beans, JScript, etc.).

If also allows you to control member access. If you use fields, you can't control whether other classes can write to it (you could use the readonly keyword, but then your field can only be initialized in the static or an instance constructor, which isn't always desired. To do this, you merely use the get accessor and don't use the set accessor (so other code can't change it via that property). There is many, many reasons to use properties over fields.

 

-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
GeneralRe: Ah, Properties! Pin
tom7625-Nov-03 4:13
tom7625-Nov-03 4:13 
GeneralRe: Ah, Properties! Pin
leppie25-Nov-03 7:23
leppie25-Nov-03 7:23 
GeneralC# hook API Pin
Anonymous24-Nov-03 22:00
Anonymous24-Nov-03 22:00 
GeneralRe: C# hook API Pin
AJ12324-Nov-03 23:29
AJ12324-Nov-03 23:29 
GeneralWindows XP Icons Pin
David M. Kean24-Nov-03 18:42
David M. Kean24-Nov-03 18:42 
GeneralRe: Windows XP Icons Pin
John R. Shaw24-Nov-03 19:39
John R. Shaw24-Nov-03 19:39 
GeneralRe: Windows XP Icons Pin
Heath Stewart25-Nov-03 3:05
protectorHeath Stewart25-Nov-03 3:05 
QuestionProperty as an array? Pin
LenHodder24-Nov-03 15:37
LenHodder24-Nov-03 15:37 
AnswerRe: Property as an array? Pin
Heath Stewart24-Nov-03 15:56
protectorHeath Stewart24-Nov-03 15:56 
GeneralRe: Property as an array? Pin
LenHodder24-Nov-03 16:10
LenHodder24-Nov-03 16:10 
GeneralRe: Property as an array? Pin
Heath Stewart24-Nov-03 16:14
protectorHeath Stewart24-Nov-03 16:14 
QuestionHELP X_x;;; I wanna make a simple program do simple task but... how? Pin
C# n00b24-Nov-03 12:42
C# n00b24-Nov-03 12:42 
AnswerRe: HELP X_x;;; I wanna make a simple program do simple task but... how? Pin
Christian Graus24-Nov-03 13:05
protectorChristian Graus24-Nov-03 13:05 
AnswerRe: HELP X_x;;; I wanna make a simple program do simple task but... how? Pin
Kentamanos24-Nov-03 13:10
Kentamanos24-Nov-03 13:10 
GeneralRe: HELP X_x;;; I wanna make a simple program do simple task but... how? Pin
C# n00b24-Nov-03 13:17
C# n00b24-Nov-03 13:17 
GeneralRe: HELP X_x;;; I wanna make a simple program do simple task but... how? Pin
Kentamanos24-Nov-03 13:33
Kentamanos24-Nov-03 13:33 
GeneralRe: HELP X_x;;; I wanna make a simple program do simple task but... how? Pin
C# n00b24-Nov-03 14:25
C# n00b24-Nov-03 14:25 

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.