1 / 16

Classes and Namespaces

Classes and Namespaces. Defining objects in C#. Overview. Namespaces Used to organize code Prevent inadvertent name duplication Classes Create a definition for what an object looks like Analogies: Database table definition vs. the actual data Blueprint vs. the actual building

mbenavides
Télécharger la présentation

Classes and Namespaces

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Classes and Namespaces Defining objects in C#

  2. Overview • Namespaces • Used to organize code • Prevent inadvertent name duplication • Classes • Create a definition for what an object looks like • Analogies: • Database table definition vs. the actual data • Blueprint vs. the actual building • Recipe vs. the actual meal

  3. Typical Class Definition File Identifies other namespaces used within the file Every project is given its own namespace within a solution One or more class definitions in a namespace Automatically created when you choose Project|Add Class within your solution. More complex versions are created when you add a Windows form (itself a class) or other complex object.

  4. Relationship of Files to Classes • A solution is a collection of projects • Usually one, for our purposes • A project consists of a collection of files in the same namespace • Objective is to build an application or library that can be linked to other applications • A class is defined across one or more files • Multiple file (partial class) definitions normally used when programmer & generated code are both present • e.g., a Windows Form

  5. Example: MainWindow Partial Class Code that is edited by the user. System generated from diagram.

  6. Class Definition • public class Class-Name • { • Data-member-declarations • Function-member-declarations • } • Comments: • Classes are declared within the project namespace • Classes are normally given public access, meaning they are visible to other classes in the namespace • Data and function members can appear in any order. Are also declared public or private (the default). Members declared private can be accessed only within the class itself. • Members declared static do not apply to individual objects. Most common example: general-purpose functions

  7. Example

  8. UML Class Diagram Class name Data members Function members Comments: + indicates public accessibility - Indicates private accessibility

  9. C# BranchOffice Class • Comments: • Class was defined within a namespace • Profit and ProfitPercent members are properties • Reset() member changes values in the class • The static members are not defined in this example

  10. Properties • In C#, a property is a member function that looks like data • Sometimes referred to as an accessor function • get and set keywords are used to identify how values are established and retrieved • If get or set is omitted, member can be made read-only (no set) or write-only (no get) • Generally, it’s a good idea to make all data members private, then define property members for them

  11. Why Define Properties? • Makes code easier to modify if you change how data is stored • Allows validation to be added as needed • Tells .NET the nature of the member, which can be used for: • Editing controls • Loading/Saving

  12. Example Accessor Properties • Comments: • Normally, a data property will be associated with a date store (e.g., m_sales, m_cogs) • The keyword value is used to identify the argument in a set statement • Sales property, as shown, does nothing beyond a public data member • COGS property limits what user can set it to (e.g., to protect against 80.00 being entered for 80% instead of 0.8).

  13. Revised UML Class Diagram

  14. Member Functions • Inside member functions, other member data, properties and functions can be defined in two ways: • By name • this.Name (Advantage: invokes the auto-completion function) • Examples—do the same thing:

  15. Interface vs. Implementation • Interface is portion of the class that other programmers use • public properties • public functions • Implementation is the ‘guts’ of the class—what the class designer needed to do to make it work • private data • private functions

  16. Parting words… • A project is a collection of classes, usually defined within the same namespace • Class definitions consist of data declarations and function definitions • Properties are functions made to look like data (using get and set keywords) • public and private keywords control access to members from other classes • In general, access your data through public properties

More Related