Classes and Multiform Projects
Classes and Multiform Projects
Classes and Multiform Projects
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
9.1 Introduction to Classes
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
Creating a Class
Class headers starts with the keyword class, followed by the name
of the class.
Member declarations are statements that define the classs fields,
properties, and/or methods.
A class may contains a constructor, which is special method
automatically executed when an object is created.
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
Sample Code
class Coin
{
private string sideUp; // field
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
Creating an Object
where,
myCoin is a variable that references an object of the Coin class;
the new keyword creates an instance of the Coin class; and
the = operator assigns the reference that was returned from the
new operator to the myCoin variable.
Once a Coin object is created, you can access members
of the class with it. E.g.
myCoin.Toss();
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
Where to Write Class
Declarations
In C# you have flexibility in choosing where to write class
declarations. E.g.
To create the Coin class, you can:
Save the class declaration is a separated .cs file; or
Add the Coin class next to the Form1 class inside the Form1.cs file.
Namespace Example
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
.
}
class Coin
{
.
}
}
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
Passing an Object to a Method
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
9.2 Properties
set
{
_name = value;
}
}
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
The Backing Field
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
9.3 Parameterized Constructor &
Overloading
A constructor that accepts arguments is known as parameterized
constructor. E.g.
public BankAccount(decimal startingBalance) { }
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
Overloading Methods
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
Overloading Constructors
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
9.4 Storing Class Type Objects in
Array & Lists
Objects that are instances of a class can be
stored in an array. E.g.
Const int SIZE = 4;
CellPhone[] phone = new CellPhone[SIZE];
phone[0] = new CellPhone();
phone[1] = new CellPhone();
.
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
Lists of Class Type Objects
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
9.5 Finding the Classes & their
Responsibilities in a Problem
When developing an object-oriented program, you need to identify
the classes that you will need to create.
One simple and popular techniques involves the following steps:
Get a written description of the problem domain.
Identify all the nouns (including pronouns and noun phrases) in the
description. Each of these is a potential class.
Refine the list to include only the classes that are relevant to the
problem.
Once the classes have been identified, you need to identify each
classs responsibilities. The responsibilities are:
The things that the class is responsible for knowing
The actions that the class is responsible for doing
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
Example
In the textbook, there are three classes: Customer, Car, and
ServiceQuote.
The Customer class has the following actions:
Create and initialize an object of the Customer class.
Get and set the customers name.
Get and set the customers address.
Get and set the customers telephone number.
The Car class has the following actions:
Create and initialize an object of the Car class.
Get and set the cars make.
Get and set the cars model.
Get and set the cars year.
The ServiceQuote class has the following actions:
Create and initialize an object of the ServiceQuote class.
Get and set the estimated parts charges.
Get and set the estimated labor charges.
Get and set the sales tax rate.
Get the sales tax.
Get the total estimated charges.
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
9.6 Creating Multiple Forms in a
Project
Every project that has a form in a Visual C# has a class.
The default name of the form is Form1 and its class is also
named Form1 which is stored in the Form1.cs file.
A Visual C# project can have multiple forms.
Each form has its own class that can be instantiated and
displayed on the screen.
When you add additional forms to a project, you add
additional classe(s), which are stored in their own files.
When you create event handler for a specific forms
controls, you write them as methods in the forms class.
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
Displaying a Form
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
Accessing Control on a Different
Form
Controls provided by the .NET Framework have public access, even
when they are placed on a form.
Code that is outside the forms class can still have access to these
controls.
For example, you can create an instance of GreetingsForm, yet
assign a value to a Label control of another form.
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
Modal and Modeless Forms
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
Flow of Executions
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.