As people we have the need to label things in order to classify and give meaning to things. We label thinks such as customer, person, product, car, dog, cat. In each item we label we can generally describe it by saying a customer has an account with us, buys products from us and pays for the product. A customer must also have a name and an account number, we need to bill the customer and need a billing address. In the English language, we use nouns to classify things.
However, in classifying things we are still generalising on the item. For example, should we say "Please deliver the products to the customer.", in conversation we fully understand the statement and it makes sense. However what customer, at what address and when? What are the products to deliver and which product should we take from the same shelf of products (the one at the top or the one at the bottom)?
In programming, we too classify the same things however there is a formal way of declaring items. We create classes to classify many things in code.
Some examples of classes
- Customer, Product, Account, Address
- Player, Enemy, High scores, Weapon
- Page, Form
- Country, Region, City
- Button, Label, Dialog box
Describing classes
In each of the above example classes we could generally describe what each one should be and how it should behave. We can also perform tasks on the class or even be made aware that some action or event has occurred.
Classes can be described using
- Properties
- Fields
- Methods
- Events
- and then some more in later topics
Properties
A property exposes characteristics or attributes of a classes, thus allowing us to set and retrieve information about the class. For example, a customer may have property for their name and another for the account number.
Properties may be read\write such as the telephone number of the customer which can be viewed or changed at any time. Other properties may be read only in which we can only view the data but never change it, such as an account number. Finally some properties may be write only where we could change it, however never view it; such as a password.
The code below creates a simple read\write property for the customers name
Methods
Methods allow us to perform actions on the class and these actions typically describe some small business or system process. These methods are normally defined by the verbs we use on the class. We might perform actions on the customer such as Change Details, Suspend, Unsuspend, Remove, Change Address, Log interaction, Fax, Bill, Accept Payment. This list is endless and needs to be defined against what your application needs to do.
The sample below implements a Print method to write the customers details to screen.
Fields
In our class we create properties and methods that interact with the data of the class, however we will need to define variables within the class to store the data. Exposing our variables as properties in a class does not provide control as we cannot control read only, read\write and write only. Secondly we can perform other actions such making the class aware that data has changed and it needs to be re-saved or even validated. Sometimes we may even perform some simple calculations such as recalculating the total when either the quantity, unit price or discount changes.
Events
As we write more complex applications, the need for other system components being dependant on our class increases. In many cases these other components would like to be notified whenever a certain event occurs. For example a button programmer will create a Click event to indicate a mouse press over the control. A business class such as a customer may raise an event indicating the customer is overdrawn or credit limit reached.
Once these classes have been defined and the events created, typically methods within the class will cause the event to be fired for example as the customer purchases products, the outstanding balance is increased by the amount of the purchase. As soon as the outstanding balance is going to exceed the available credit the CreditLimitReached event is fired.
Example
The code below shows an example of a customer class with a Name property and field. It also demonstrates a Print method. In future lessons other components of a class will be explained.
namespace BrettM.Training.OO.Classes
{
using System;
/// <summary>
/// The customer class defines a business client that purchases
/// products and services from our company.
/// </summary>
class Customer
{
#region // Fields
/// <summary>
/// A field variable to hold the customers name
/// </summary>
private string _name;
#endregion
#region // Properties
/// <summary>
/// The name of the customer
/// </summary>
public string Name
{
// The get accessor to return the name field
get { return _name; }
// A set accessor to change the name field
set { _name = value; }
}
#endregion
#region // Methods
public void Print()
{
Console.WriteLine("Customer details");
Console.WriteLine("----------------");
Console.WriteLine("Name: {0}", this.Name);
}
#endregion
}
/// <summary>
/// The test program
/// </summary>
class Program
{
static void Main(string[] args)
{
// Create a new object instance of the customer
// and set refer to it as 'cust'
Customer cust = new Customer();
// Set the name property
cust.Name = "Mr Tom Jones";
// Execute the print method
cust.Print();
}
}
}