Article
ASP.NET 2.0: A Getting Started Guide
Constructors
Constructors are special kinds of method are that used to initialize the object. In OOP, when we create new instances of a class, we say we're instantiating that class. The constructor is a method of a class that's executed automatically when a class is instantiated.
At least one constructor will be defined for most of the classes you will write (though we can define more than one constructor for a class, as we'll see shortly), since it's likely that some data will need to be initialized for each class at creation time.
In C# and VB, the constructor is defined as a method that has the same name as the class, and has no return type.
Scope
You should now understand programming objects to be entities that exist in a program and are manipulated through the methods and properties they expose. However, in some cases, we want to create for use inside our class methods that are not available to code outside that class.
Imagine we're writing the Sit method inside this class, and we realize that before the dog can sit, it has to shuffle its back paws forward a little (bear with me on this one!). We could create a method called ShufflePaws, then call that method from inside the Sit method. However, we don't want code in an ASP.NET page or in some other class to call this method--it'd just be silly. We can prevent it by controlling the scope of the ShufflePaws method.
Carefully controlling which members of a class are accessible from outside that class is fundamental to the success of object oriented programming. You can control the visibility of a class member using a special set of keywords called access modifiers:
Public- Defining a property or method of a class as public allows that property or method to be called from outside the class itself. In other words, if an instance of this class is created inside another object (remember, too, that ASP.NET pages themselves are objects), public methods and properties are freely available to the code that created that instance of the class. This is the default scope for VB and C# classes.Private- If a property or method of a class is private, it cannot be used from outside the class itself. So, if an instance of this class is created inside an object of a different class, the creating object has no access to private methods or properties of the created object.Protected- A protected property or method sits somewhere between public and private. A protected member is accessible from the code within its class, or to the classes derived from it. We'll learn more about derived classes a bit later.
Deciding which access modifier to use for a given class member can be a very difficult decision--it affects not only your class, but also the other classes and programs that use your class. Of special importance are the class's public members, which together form the class's public interface. The public interface acts like a contract between your class and the users of your class, and if it's designed properly, it shouldn't change over time. If, for example, you mark the Sit method as public, and later decide to make it private, all the other classes that use this method will have to change accordingly, which is not good. For an extreme scenario, imagine that in a year, Microsoft decided to remove the ToString method from its classes--obviously, this would wreak havoc with your code.
Keep Everything Private until you Need It
As a simple guideline for designing your classes, remember that it's often easier just to make all the members private, and make public only those that really need to be public. It's much easier to add to a public interface than it is to remove from it.
Events
We've covered events in some depth already. To sum up, events occur when a control object sends a message as a result of some change that has been made to it. Generally, these changes occur as the result of user interaction with the control via the browser. For instance, when a button is clicked, a Click event is raised, and we can handle that event to perform some action. The object that triggers the event is referred to as the event sender, while the object that receives the event is referred to as the event receiver. You'll learn more about these objects in Chapter 4.
Understanding Inheritance
The term inheritance refers to the ability of a specialized class to refine the properties and methods exposed by another, more generalized class.
In our dog example, we created a class called Dog, then created instances of that class to represent individual dogs such as Rayne. However, dogs are types of animals, and many characteristics of dogs are shared by all (or most) animals. For instance, Rayne has four legs, two ears, one nose, two eyes, etc. It might be better, then, for us to create a base class called Animal. When we then defined the Dog class, it would inherit from the Animal class, and all public properties and methods of Animal would be available to instances of the Dog class.
Similarly, we could create a new class based on the Dog class. In programming circles, this is called deriving a subclass from Dog. For instance, we might create a class called AustralianShepherd, and one for my other dog, Amigo, called Chihuahua, both of which would inherit the properties and methods of the Dog base class, and define new classes specific to each breed.
Don't worry too much if this is still a little unclear. The best way to appreciate inheritance is to see it used in a real program. The most obvious use of inheritance in ASP.NET is in the technique called code-behind, and we'll build plenty of examples using inheritance and code-behind in Chapter 4, Constructing ASP.NET Web Pages.
Objects In .NET
If this is the first book in which you've read about object oriented programming, you've probably started to dream about objects! Don't worry, the effect of first exposure to objects doesn't usually last for more than a week. Even though this is yet another discussion about objects, I promise it won't be boring. Moreover, in the course of this section, we'll cover some important concepts that every serious .NET programmer must know.
So far, we've explored various concepts that apply in one form or the other to almost any truly object oriented language. Every language has its peculiarities, but the general concepts are the same in all of these languages.
You may already have heard the common mantra of object oriented programmers: "everything is an object." This has two meanings. First of all, in C#, every program consists of a class. In all stages of application development, from design to implementation, decisions must be made in regard to the way we design and relate objects and classes to each other. Yes, objects are everywhere.
.NET extends this to yet another level, giving the phrase "everything is an object" extra meaning. In the world of .NET, every class ultimately derives from a base class named Object, so "everything is an object" becomes "everything is an Object."
If you look at the documentation for the ASP.NET Page class, you can see the list of classes from which this class inherits, as shown in Figure 3.7.

Figure 3.7. The Page class's documentation
You'll remember from the last section that we said our hypothetical AustralianShepherd class would inherit from the more general Dog class, which, in turn, would inherit from the even more general Animal class. This is exactly the kind of relationship that's being shown in Figure 3.7?Page inherits methods and properties from the TemplateControl class, which in turn inherits from a more general class called Control. In the same way that we say that an Australian Shepherd is an Animal, we say that a Page is a Control. Control, like all .NET classes, inherits from Object.
Since Object is so important that every other class derives from it, either directly or indirectly, it deserves a closer look. Object contains the basic functionality that the designers of .NET felt should be available in any object. The Object class contains these public members:
EqualsReferenceEqualsGetHashCodeGetTypeToString
The only member we're really interested in at this moment is ToString, which returns the text representation of an object. This method is called automatically when conversions to string are needed, as is the case in the following code, which joins a number and a string:
Dim age As Integer = 5
Dim message As String = "Current Age: " & age
int age = 5;
string message = "Current Age: " + age;
Namespaces
As ASP.NET is part of the .NET Framework, we have access to all the goodies that are built into it in the form of the .NET Framework Class Library. This library represents a huge resource of tools and features in the form of classes; these classes are organized in a hierarchy of namespaces. When we want to use certain features that .NET provides, we have only to find the namespace that contains the desired functionality, and import that namespace into our ASP.NET page. Once we've done that, we can make use of the .NET classes in that namespace to achieve our own ends.
For instance, if we wanted to access a database from a page, we would import the namespace that contains classes for this purpose, which could be System.Data.SqlClient. You can view the namespace of a class when visiting its page in the .NET documentation. For example, the Button control's class can be found in System.Web.UI.WebControls.
To use a class that's part of a namespace that isn't available to you by default, you either need to import the namespace, or reference the class using its fully qualified name, such as System.Web.UI.WebControls. To import a namespace page, we use the Imports directive in VB, and using in C#:
Imports System.Data.SqlClient
using System.Data.SqlClient;
As we've imported that namespace, we have access to all the classes that it contains.