Home » ASP.NET Basics » 06 - ASP.NET 2.0 Server Controls
6

How do we create a class?

How to create classes

A class is defined using the keyword `class'. The syntax is -

 access-modifier class classname { define properties and functions of the class here } 

For example -

 public class Person { } 

In the above example, we have created a class named `Person'. The keyword `public' is the access-modifier. The `public' keyword indicates that there is no restriction on accessing this class. Other access-modifiers are - private, protected, internal and protected internal. However, we will stick with `public' access-modifier, as we want our classes to be accessed without restriction.

The class that we have defined is an empty class. It does not contain anything. We will now add some fields to this class.

 public class Person { private string name; private int age; private string gender; }

We have added some variables inside the class. They are known as the fields. Fields store the data for the class. But there is one problem. Notice the keyword `private' in every field declaration. This implies that we cannot access these variables directly in our application. In other words, we cannot use them in our code with statements like -

 age=32;

Why have we made them private? How can we access them for storing data? One way is to make them public i.e. use the keyword `public' in place of `private'. But the principles of object-oriented programming suggest that the fields of a class should be private. So what is the solution?

The solution is to use Properties. Let us modify the class -

 public class Person { private string name; private int age; private string gender; public string Name { get { return name; } set { name=value; } } 

We have added a property to the class called `Name'. this property is used to store value in the `name' field. Note that it starts with capital `N' while the field `name' starts with a small `n'. It is a common practice to name the fields in such a way that the the first letter is in lowercase, while properties use uppercase. Since C# is a case sensitive language, `name' and `Name' are two different entities.

The property is public so it can be accessed by our application. Inside the property, we have a `get' block and a `set' block. The `set' block is used to store values in the fields. The statement:

 name= value;

will store a value in the field `name'.

The `get' block returns the value stored in a field. In our example, it will return the value stored in the `name' field.

We will add two more properties. One for the `age' field and one for the `gender' field -

 public class Person { private string name; private int age; private string gender; public string Name { get { return name; } set { name=value; } } public string Gender { get { return gender; } set { gender=value; } } public int Age { get { return age; } set { age=value; } } } 

Now our class has three properties to access the fields. Before moving ahead, let us see how we implement the class in practice i.e. where we write the code and how we can use the class in our application.

1. Open the CodeSample project. In the solution explorer, right click on the project name. Move the mouse to the Add ASP.NET folder option and then select the App_code folder name from the sub menu.

Note: Though we can define a class in the code-behind file for our web page, it is recommended that we create classes inside a special ASP.NET folder called App_Code. That is why we have created this folder.

2. The App_Code folder is now added to the project. Right click on the folder and from the menu, select the Add New Item... option. A dialog box will appear. In this dialog box, select class in the Templates section. Provide a name for the class but do not change the .cs extension. Click on the Add button to continue.

3. A new file will appear in the IDE - App_Code/Person.cs. As we can see in the code, a class named `Person' is added:

4. There is some code inside the class but we can ignore it for now. Write the class definition code as we have seen earlier, with fields and properties.

The complete code should be like this -

 using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; /// <summary> /// Summary description for Person /// </summary> public class Person { //These are fields private string name; private int age; private string gender; // These are properties public string Name { get { return name; } set { name=value; } } public string Gender { get { return gender; } set { gender=value; } } public int Age { get { return age; } set { age = value; } } // we will ignore the following for now public Person() { // // TODO: Add constructor logic here // } }

Congratulations! We have created our very own class. Save the file. We will now see how we can use this class in our application.

1. Add a new Web form to the project. Let us call it TestPerson.aspx. Remember that we are using the `code behind model' so do not forget to check the Place code in separate file check box while adding the page.

2. Switch to the design view of the page. Drag and drop three text boxes, one button and four label control as shown below:

3. Set the Text properties of the controls as shown in the figure. Also, set the visible property of the bottom-most label control to False.

4. Double click on the Button control. The event handler for the Button's click event will become available in the code behind file:

Write the following code inside the event handler. Note that when we type the code, the IntelliSense displays a list of available properties for our object:

 Person p = new Person(); p.Name = TextBox1.Text; p.Gender = TextBox2.Text; p.Age = Convert.ToInt32(TextBox3.Text); Label4.Text = "Your name is " + p.Name + ". You are " + p.Age + " years old " + p.Gender; Label4.Visible = true; 

Let us understand what this code is programmed to do. The first statement is:

 Person p = new Person();

In this statement, we are creating an object of class `Person'. `p' is the name of our object. It is like a variable and its data type is the class. The syntax for object declaration is:

 class-name object = new class-Name(); 

Here, Class-name is the name of the class. Object is the name of the object that we want to create. `new' is the keyword used for object declaration.

So the first statement of the code declares an object `p' of the class `Person'. Objects are often called `Instances'.

We can declare more than one object of a class. Each object will have the same properties but with different values. For example,

 Person p1 = new Person(); Person p2 = new Person(); 

Here, p1 and p2 are the objects of the Person class so each of them will have a name, age and gender. However, the values of name, age and gender can be different for p1 and p2.

The next three statements are used to set the Name, Gender and Age properties of the object. The values entered in the text boxes are assigned to the properties. The properties, in turn, will store these values in the private fields of the class. Here, the `set' block of the properties is used.

The following statement gathers the values stored in the private fields of the class with the help of the properties. For example, p.Name will get the value stored in the private field `name' for the object p. Here, the `get' block of the properties is used. The values of name, gender and age are gathered by the respective properties. They are combined with a message and assigned to the Text property of the Label control.

Finally, the last statement sets the visible property of the Label control to true so that the Label can display the message on the page.

Run and test the page. This is a sample output:

This was not a very suitable example of using a class because we can get the user input and display it back without using a class. However, this simple example illustrates how we can create and use a class.

Let us review what we have learned in the above code sample. We define the class, preferably in a separate class file. Inside the class, we declare fields and properties. Fields are private so we cannot access them directly in our program. To store and read values from the fields, we use properties. Properties have a get block to read value from the field and a set block to store value in the field. Once the class is defined, we can create objects of the class using the syntax:

 class-name object = new class-Name(); 

The object can access the properties of the class using the dot (.) operator. The syntax is:

 Object.property-name