Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Build Your Own ASP.NET 2.0 Web Site Using CSharp And VB (2006) [eng]-1.pdf
Скачиваний:
142
Добавлен:
16.08.2013
Размер:
15.69 Mб
Скачать

Chapter 3: VB and C# Programming Basics

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.

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.

84

Objects In .NET

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:

Equals

ReferenceEquals

GetHashCode

GetType

85

Chapter 3: VB and C# Programming Basics

ToString

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:

Visual Basic

Dim age As Integer = 5

Dim message As String = "Current Age: " & age

C#

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#:

Visual Basic

Imports System.Data.SqlClient

C#

using System.Data.SqlClient;

86