Home

Introduction to Classes

 

Fundamentals of Classes

 

Introduction

In the previous lesson, to use a variable, we were declaring it using a known and simple data type. For example, we could use an integer to declare a variable that represents the number of bedrooms of a house. Here is an example:

double side = 4;

As opposed to a simple variable, in C#, you can use one or more variables to create a more complete or complex object.

In the previous lessons, we saw how to add new files to an ASP.NET application. In the same way, you can create a file whose main purpose is to hold a class. Before creating such a file, you must have a folder named App_Code in your project. To get it, you have two options:

  • You can first create the folder. To do that, you can right-click the name of the project in the Solution Explorer, click New Folder, and name it App_Code. To create the file, you can right-click the App_Code folder in the Solution Explorer, click Add New Item..., select Class, accept the suggested name or give it a new one, and click Add
  • You can start creating the file. To do that, on the main menu, you can click Web Site -> Add New Item..., select Class, accept the suggested name or give it a new one, and click Add. You would then receive a message box asking you whether to create a folder named App_Code
     


    You can the click Yes. If you do, a folder named App_Code would be added to your project and the new class would be created in that folder.

To create such a file, on the main menu, you can click Web Site -> Add New Item... In the Templates list, click Class, accept or change the name of the file. You can omit the .cs extension. If you do, a file with the extension .cs would be added.

Anatomy of a Class

A class is a technique of using one or a group of variables to be used as a foundation for a more detailed variable. To create a class, you start with the class keyword followed by a name and its body delimited by curly brackets. Here is an example of a class called House:

class Square
{
}

When a project is made of various files, each file is represented by a tab in the top section of the Code Editor. To access a file, you can click its tab.

Visually Managing Classes

To assist you with managing the various classes of a project, Microsoft Visual Studio includes various tools and windows. One of the windows you can use is called the Class View. To display it, on the main menu, you can click View -> Class View:

The Class View is made of four sections. The toolbar under the title bar includes four buttons. To identify a button, you can position the mouse on it and a tool tip would appear.

Under the toolbar, another bar made of a combo box and a button allow you to search.

The main top section of the Class View is made of various nodes. To expand a node, you can click its + button. To collapse a node, you can click its - button. The top node displays the name of the web site. Under the project node, the names of classes display. The bottom section of the Class View is used to display the members of a class. To see the members of a class, you can click it in the top window. Here is an example:

The Class View

 

Declaring a Variable of a Class Type

Like any normal variable, to use a class in your program, you can first declare a variable for it. Like the variables we introduced in the previous lesson, to declare a variable of a class, type its name followed by a name for the variable. For example, to declare a variable of the above Square class, you could type the following:

void btnCalculate_Click(object sender, EventArgs e)
{
    Square sqr;
}

The variables we have declared so far are called value variables. This is because such variables of primitive types hold their value. The C# language supports another type of variable. This time, when you declare the variable, its name doesn't hold the value of the variable; it holds a reference to the address where the actual variable is stored in memory. This reference type is the kind used to declare a variable for a class.

To use a variable as reference, you must initialize it using an operator called new. Here is an example:

void btnCalculate_Click(object sender, EventArgs e)
{
    Square sqr;

    sqr = new GeoSquare();
}

You can also use the new operator directly when declaring the variable as follows:

void btnCalculate_Click(object sender, EventArgs e)
{
    Square sqr = new GeoSquare();
}

In C#, as well as Visual Basic, if you create a class in any of the files that belong to the same project, the class is made available to all other files of the same project.

Sharing Classes

If you want your class to be accessible to code written in other languages, precede the class keyword with public when creating it. Here is an example:

public class GeoSquare
{
}

Class' Fields

 

Introduction

Consider a class named Square:

public class GeoSquare
{
}

The section between the curly brackets, { and }, of a class is referred to as its body. In the body of a class, you can create a list of the parts that make up the class. Each of these parts must be a complete variable with a name and a data type. Here is an example:

public class GeoSquare
{
    double sd;
}

The variables declared in the body of a class are referred to as its member variables and each member variable is referred to as a field. The fields can be any type we have seen in the previous lesson. When creating a class, it is your job to decide what your object is made of.

Accessing Class Members

The parts of an object fall into two main categories: those you can touch and those you don't have access to. The parts of an object that you have access to are referred to as public; those you can't see or touch are referred to as private.

A C# class also recognizes that some parts of a class can be made available to other classes and some other parts can be hidden from other classes. A part that must be hidden from other classes is private and it can be declared starting with the private keyword. If you declare a member variable and want to make it available to other classes, you must start its name with the public keyword. The public and private keywords are referred to as access level.

By default, if you declare a member variable (or anything else) in a class but don't specify its access level, the member is considered private and cannot be accessed from outside, that is by a non-member, of that class. Therefore, to make a member accessible by other classes, you must declare it as public.

You can use a mix of public and private members in a class and there is no rule on which access level should be listed first or last. Just keep in mind that if you omit or forget the access level of a member of a class, the member is automatically made private.

After declaring a member of a class, to access it from another class, first declare a variable from its class as we saw earlier. To actually access the member, use the period operator "." as follows:

void btnCalculate_Click(object sender, EventArgs e)
{
    Square sqr = new GeoSquare();

    double OneSide = sqr.side;
}

To reduce confusion as to what member is public or private, we will always specify the access level of a member variable.

 

Previous Copyright 2007 FunctionX, Inc. Next