160 likes | 182 Views
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
E N D
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 • Recipe vs. the actual meal
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.
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
Example: MainWindow Partial Class Code that is edited by the user. System generated from diagram.
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
UML Class Diagram Class name Data members Function members Comments: + indicates public accessibility - Indicates private accessibility
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
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
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
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).
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:
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
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