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

Understanding Object-Oriented Programming

Definition and overview of object-oriented programming

Just the mention of object-oriented programming scares away many would-be developers. However, every modern programming language supports and encourages the use of object-oriented programming. Once we understand the concepts and know how to use them, we will find that it is the most efficient programming method available.

Another reason why we want to know about object-oriented programming is that the .NET framework relies heavily on it. To make better use of the .NET framework and take advantage of all its features, it is necessary to have at least a basic understanding of object-oriented programming.

Before we proceed, note that the object-oriented programming is a detailed concept and we cannot cover everything here. In this part, we will learn the absolute essentials about object-oriented programming.

Object-oriented programming is based on the concept of objects and classes. Objects are entities with properties and methods (functions). In real life, we see many objects around us. A simple example is a car. A car has properties such as its color and model. A car also has functions like start and stop.

If we consider what we have learned so far, especially about ASP.NET controls, we will see that every control (button, textbox or any other control) has properties and functions. So are they objects? Definitely they are. Every control is an object. Even the data types are objects. Each of these objects have some properties and functions. We can create our own objects if needed.

That is where classes are used. We define the properties and functions of an object inside a class. Once the class is defined with required properties and functions, we can create objects of the class. All the objects of a class will have the same properties and methods. Hence, we can say that a class acts like a blueprint for creating objects.

Wait a minute! We need to define classes to create objects. But we have not created any class to use the controls or data types. They are objects, aren't they?

Well, yes. Controls and data types are indeed objects. We have not created any classes for them because the .NET framework has already created the required classes for us. If we examine the code-behind file of a web page, we will find the following segment of code at the top:

 using System; using System.Data; using System.Configuration; using System.Collections; 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; 

What we see here is known as a namespace. A namespace is a logical collection of code, mainly classes. When we use a namespace in our program, we have access to all the classes in that namespace and we can use the objects of those classes. For example, the System.Web.UI.WebControls namespace contains the classes needed for using ASP.NET server controls on our page. The `using' keyword indicates that we want to use this namespace in our code.

The .NET framework has many such namespaces for different purposes. The System.Data namespace, for example, contains the classes for data handling. All we need to do is to include the required namespace in our program to access the classes in that namespace.

Note: namespaces can contain other elements like interfaces, enumerators and structures apart from classes.

To use a namespace in our program, we use the following syntax -

 using namespace;

For example -

 using System.Data;

If we want to include more than one namespace, we need to write multiple statements, each on a separate line.