Object Inheritance
Objects are everywhere. Everything in .NET is an object. There are thousands of classes in the .NET Framework, ready to be instantiated into objects in the world's greatest Web application that you're going to write. Can you imagine having to write the code from scratch for every one of those classes? I can't either, and because of inheritance, the clever folks in Redmond avoided this duplication of effort.One of the great things about inheritance is that you get something for free, including all of the benefits that go along with it. When you inherit a class, your new class gets all of that functionality instantly, plus whatever new functionality you pile on top of it.When you browse the .NET SDK documentation, every class shows that it is derived from another class. These classes inherit members and structure from another class, for better or worse, much in the way that you might inherit blue eyes or heart disease from your parents. In .NET, everything ultimately inherits from System.Object, the mother of the entire class library. It has a couple of methods and a constructor that is inherited by every single class.Keeping with our car analogy, let's say that we want our car to be more secure. We want to replace the unlock code with our own system, and we want to add keyless entry. It would be silly to design our new car from scratch because we already have a Car class. We'll create a new class that inherits the old car as our base class, replace the Unlock() method, and add an entirely new method called WirelessUnlock().
Listing 1.4. Unlocking your car
C#
VB.NET
public class BetterCar : Car
{
public override void Unlock(string newCode)
{
// better unlock code here
}
public void WirelessUnlock(string newCode)
{
Unlock(newCode);
}
}
Let's look at each part of the code. The beginning looks just like any other class declaration, except that we indicate that we'll inherit from the Car class. You've seen this before if you've used a code-behind class for your v1.x ASP.NET pages, where you inherit from System.Web.UI.Page, thus making available all of the events and properties common to the page class.Next we declare our own Unlock() method by overriding it. Any time code instantiates our BetterCar class, our replacement Unlock() method will be used instead of the original version.
Public Class BetterCar
Inherits Car
Public Overrides Sub Unlock(newCode As String)
' better unlock code here
End Sub
Public Sub WirelessUnlock(newCode As String)
Unlock(newCode)
End Sub
End Class
The old Unlock() isn't actually gone. We can still get to it by referring to base.Unlock() (or MyBase.Unlock() in VB.NET). This is handy when you want the base implementation to do something else in addition to the original action. For example, if we wanted to leave the Unlock() implementation the same and flash the lights, we might write our new method like this:C# VB.NET
|
Listing 1.5. Assigning a value to a Label control
For the moment, don't worry about the code syntax; just take note that this user control has a property called NewLabelText. Now let's use this user control in a page, shown in Listing 1.6.
<%@ Control Language="c#" %>
<script runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
MyLabel.Text = _NewLabelText;
}
private string _NewLabelText;
public string NewLabelText
{
get {return _NewLabelText;}
set {_NewLabelText = value;}
}
</script>
<asp:Label id="MyLabel" runat="server" />
Listing 1.6. Using a user control in a page
[View full width]
Don't look now, but you just practiced inheritance! Your user control inherits from the UserControl class. ASP.NET performs this inheritance for you, and while you may not be aware of it, it is going on under the hood. You've added a new property to the user control class called NewLabelText. When the page executes, it copies the value "Look at my text!" to the Label control in the user control.Let's look at the same example from another point of view. Let's say that you want several user controls to inherit from a base class that has this special property. We'll create a class that inherits from UserControl to accomplish this (Listing 1.7).
<%@ Page language="c#" %>
<%@ Register TagPrefix="uc1" TagName="WebUserControl1" Src=" %>
<html>
<body>
<form id="Form1" method="post" runat="server">
<uc1:WebUserControl1 id="WebUserControl11" runat="server" NewLabelText="Look at mytext!" />
</form>
</body>
</html>
Listing 1.7. Creating a class that inherits from UserControl for use by other controls
C#
VB.NET
public class BetterUserControl : System.Web.UI.UserControl
{
private string _NewLabelText;
public string NewLabelText
{
get {return _NewLabelText;}
set {_NewLabelText = value;}
}
}
Notice that we inherit UserControl in our class declaration. You can have your user controls all use this class by changing the Control directive:
Public Class BetterUserControl
Inherits System.Web.UI.UserControl
Private _NewLabelText As String
Public Property NewLabelText() As String
Get
Return _NewLabelText
End Get
Set
_NewLabelText = value
End Set
End Property
End Class
This user control will have a NewLabelText property even though we haven't declared it. In fact, just as if it was a normal user control, it will have all of the other properties (such as ID and Page) and methods (such as FindControl()) you've come to know and love. Your user control inherits from BetterUserControl, which inherits from System.Web.UI.UserControl, which inherits from System.Web.UI. TemplateControl, which inherits from System.Web.UI.Control, which finally inherits from System.Object. Get it? Table 1.1 shows the inheritance tree and the improvements made at each new derived class.
<%@ Control Language="c#" Inherits="BetterUserControl" %>
System.Object | The base from which all other objects are inherited. |
System.Web.UI.Control | Inherits Object's members, and adds a wealth of new properties (ID, EnableViewState, Visible, etc.) as well as its own methods (DataBind(), RenderControl(), etc.) and events (Init, Load, etc.). |
System.Web.UI.TemplateControl | Adds a few more members such as the LoadControl() method and Error event. Object's and Control's members are still available. |
System.Web.UI.UserControl | Many new members are added including the Request property MapPath() method, while all of the members of the inherited classes are still available. |
BetterUserControl | This is where we add our NewLabelText property. It joins the ranks of all of the other members from the underlying four inherited classes. |