Presentation is loading. Please wait.

Presentation is loading. Please wait.

Properties 7: Properties Programming C# © 2003 DevelopMentor, Inc.

Similar presentations


Presentation on theme: "Properties 7: Properties Programming C# © 2003 DevelopMentor, Inc."— Presentation transcript:

1 Properties 7: Properties Programming C# © 2003 DevelopMentor, Inc.
12/1/2003

2 Objectives Describe properties purpose definition use 7: Properties
Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

3 Public data Public data can be both good and bad
7: Properties Public data Public data can be both good and bad good because it allows clear and simple access syntax bad because it exposes implementation details of class class Person { public string name; ... } public field Person p = new Person(); p.name = "Bob"; string n = p.name; write read Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

4 7: Properties Private data Class typically has private fields and public access methods clients use methods to access fields class Person { private string name; public string GetName() return name; } public void SetName(string newName) name = newName; private field public read method public write method Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

5 Advantage: access control
7: Properties Advantage: access control Private data lets class offer only desired access methods read-only write-only read and write class Person { private string name; public string GetName() return name; } supply only read method for read-only access Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

6 Advantage: data validation
7: Properties Advantage: data validation Private data allows class to perform data validation methods can do error checking clients can not avoid validation code by accessing data directly class Person { public void SetName(string newName) if (newName == "") // error ... name = newName; } ... error checking Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

7 Disadvantage: access method names
7: Properties Disadvantage: access method names Typical to prefix access method names with Get and Set follows common wisdom to make method names verb phrases but conflicts with concept of field as representing data property Person p = new Person(); p.SetName("Bob"); string n = p.GetName(); write read Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

8 Disadvantage: access method usage
7: Properties Disadvantage: access method usage Syntax of access methods can be undesirable set does not use = so is not obviously a write operation get typically has awkward looking empty parentheses Person p = new Person(); p.SetName("Bob"); string n = p.GetName(); no assignment operator empty parentheses Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

9 7: Properties Properties Properties combine the advantages of public and private data have clean access syntax like public fields provide error checking opportunity like private fields Person has Name property Person p = new Person(); p.Name = "Ann"; string n = p.Name; write read Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

10 Structure of property definition
7: Properties Structure of property definition Property definition has declaration of access level, type, and name code block with implementation class Person { public string Name ... } declaration of Name property implementation Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

11 Properties storage Property does not provide any needed data storage
typically define field to store data class Person { private string name; public string Name ... } field to store data Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

12 Property accessors Property can define set and get accessors
7: Properties Property accessors Property can define set and get accessors definitions go inside implementation code block no explicit arguments or return types class Person { public string Name set ... } get write method read method Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

13 Set implementation Set typically records new data for property
7: Properties Set implementation Set typically records new data for property new data passed in hidden argument called value class Person { private string name; public string Name set name = value; } ... use value argument Person p = new Person(); p.Name = "Ann"; calls set Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

14 Get implementation Get typically returns current value of property
7: Properties Get implementation Get typically returns current value of property uses return to return value class Person { private string name; public string Name get return name; } ... return value of property Person p = new Person(); ... string n = p.Name; calls get Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

15 Validation Property accessors can contain validation code class Person
7: Properties Validation Property accessors can contain validation code class Person { private string name; public string Name set if (value == "") // error ... name = value; } ... validation Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

16 Access control Property may provide any access desired read-only
7: Properties Access control Property may provide any access desired read-only write-only read and write class Person { public int Age get ... } Age property supply get and omit set for read-only Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

17 Data storage Property does not need to be backed by matching field
7: Properties Data storage Property does not need to be backed by matching field value may be produced in any way that is appropriate class Person { private DateTime dob; public int Age get int age = DateTime.Today.Year - dob.Year; DateTime birthday = dob.AddYears(age); if (birthday > DateTime.Today) age--; return age; } ... store date of birth calculate age birthday occurred yet this year? Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

18 Static property Property can be static
7: Properties Static property Property can be static must be accessed through type name class Item { private static int revenue; public static int Revenue get { return revenue; } set { revenue = value; } } ... define static property Item.Revenue = 5; access using type name Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

19 Access level Property accessors have same access level
7: Properties Access level Property accessors have same access level both public, both private, etc. class Person { public string Name set { ... } get { ... } } ... public property public read method public write method Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

20 Summary Properties model traits of a class or object
provide advantages of private field / public accessor pattern with clean user syntax Programming C# © 2003 DevelopMentor, Inc. 12/1/2003


Download ppt "Properties 7: Properties Programming C# © 2003 DevelopMentor, Inc."

Similar presentations


Ads by Google