Defining a Class


Classes are the primary blocks of objectoriented programming.

When writing applications, developers often attempt to solve a problem or problem domain, as it's often called. Problem domains can be broken up into pieces and each piece can be addressed with a class. Take the example program for this chapter. The purpose of the program is to maintain a list of tasks a to-do listand to display the list to the user as requested . We could write a class called ToDoItem to store each to-do item, and another class called ToDoList, to store an entire list of to-do items.

The class definition serves as a factory for objects. Each object can represent a different entity in the application. In our example, the ToDoItem class tells the language what ToDoItem objects contain. It is up to the developer to create objects from that class by using the new operator in C#:

 ToDoItem item1 = new ToDoItem(); 

Classes have two main aspects: state and behavior. Those are two abstract terms that simply boil down to fields and functions.

Fields are variables declared inside a class. They are used to store information about each object. In the case of the application for this chapter, we create objects of type ToDoItem to hold the information for each to-do item. Fields are used to store the information about each to-do item. For example, we can add a field to store the name of the task and one to store the description. Fields enable each item object produced from the ToDoItem class to represent a different item in the list ( Figure 2.43 ). On the other hand all we need is one object of type ToDoList to hold the collection of to-do items. This ToDoList class may have a single field that refers to a collection of ToDoItems.

Figure 2.43. Each instance of the ToDoItem class can store its own item and description. If you don't set a reference variable equal to a new instance of the class or to another variable that already points to an object then the variable's value is null.

graphics/02fig43.gif

In addition to the class defining the state information for each object the developer defines the behavior for the class. This is done by adding functions that make sense when using a ToDoItem object or a ToDoList object. For example, the ToDoList object may have a function called Add and another function called Remove. A programmer can use these functions to add to-do items to the list or to remove them from the list. A to-do item may also have functions such as SetInfo.

Classes are defined with the class keyword.

To declare a class:

  1. Type the keyword class in lowercase letters followed by a space, and the name of the class (see sidebar).

  2. Add an open curly bracket after the class name.

  3. Add fields and functions as necessary.

  4. Add a closing curly bracket at the end of the class definition ( Figure 2.44 ).

    Figure 2.44 The ToDoItem class serves as a factory for ToDoItem objects. It contains fields and functions specific to the class.
      class ToDoItem   {   //fields  string item = "";    string description = "";  //function  public void SetInfo(string item,    string description)    {      this.item = item;      this.description = description;    }  }  

Class Names

Class Names are referred to as identifiers under the C# ECMA Specification (section 9.4.2). There are a very large number of rules for which characters are allowed and which aren't, too many to cover here. The most important rule is that class names begin with a letter, an underscore _ or the @ symbol. Use the @ symbol in front of the name if you wish to use a language keyword as the class name. For example, suppose you want to name your class class, you would name your class as follows : class @class . This is a lot of fun to do, but may get you dirty looks from your coworkers ( Figure 2.45 ).

Figure 2.45 Don't write code like this!
  class @new  { } class Fun {    void JobSecurity()    {  @new @new = new @new();  } } 

graphics/tick.gif Tips

  • Classes are named using a technique called intercaps. With intercaps only the first letter in each word is capitalized. So in our example the class names are ToDoItem and ToDoList starting with a capital letter.

  • Fields and functions inside of the class are referred to as members of the class. They are discussed later in this chapter.

  • Classes have a scope. They are either internal or public. You control the scope by using an access modifier in front of the class keyword. Internal classes can only be used within the current project (more accurately, the current assembly). Public classes can be used from other assemblies as well ( Figure 2.46 ).

    Figure 2.46. Classes can always be used by code in the same project (or assembly). By default they can't be used by another assembly. You can export them outside of the assembly by marking them public.

    graphics/02fig46.gif




C#
C# & VB.NET Conversion Pocket Reference
ISBN: 0596003196
EAN: 2147483647
Year: 2003
Pages: 198
Authors: Jose Mojica

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net