Maximizing ASP.NET Real World, Object-Oriented Development [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Maximizing ASP.NET Real World, Object-Oriented Development [Electronic resources] - نسخه متنی

Jeffrey Putz

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید













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#



public class BetterCar : Car
{
public override void Unlock(string newCode)
{
// better unlock code here
}
public void WirelessUnlock(string newCode)
{
Unlock(newCode);
}
}


VB.NET



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


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.





Overriding is the process of replacing one method with another in a derived class. Whenever the method is called, the original version, for all practical purposes, does not exist unless you explicitly call the original version from the new one (as we''ll discuss in a moment).



Finally, we''ve added a WirelessUnlock() method. This method also takes a key code (imagine that it''s transmitted from our WirelessRemote object), and that key code is passed off to our new Unlock() method.





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#


public override void Unlock(string newCode)
{
base.Unlock(newCode);
FlashLights();
}


VB.NET


Public Overrides Sub Unlock(newCode As String)
MyBase.Unlock(newCode)
FlashLights()
End Sub



How would you use inheritance in real life? Let''s consider the user control. The user controls that you build (ending in .ascx) are, like anything else, classes that are instantiated into objects when the code executes. Imagine for a moment that you have a user control that has a label and code to assign it a value, as in Listing 1.5.


Listing 1.5. Assigning a value to a Label control


<%@ 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" />


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.


Listing 1.6. Using a user control in a page

[View full width]



<%@ Page language="c#" %>
<%@ Register TagPrefix="uc1" TagName="WebUserControl1" Src="WebUserControl1.ascx" %>
&l166>
<body>
<form id="Form1" method="post" runat="server">
<uc1:WebUserControl1 id="WebUserControl11" runat="server" NewLabelText="Look at my
text!" />
</form>
</body>
</html>


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).


Listing 1.7. Creating a class that inherits from UserControl for use by other controls


C#



public class BetterUserControl : System.Web.UI.UserControl
{
private string _NewLabelText;
public string NewLabelText
{
get {return _NewLabelText;}
set {_NewLabelText = value;}
}
}


VB.NET



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


Notice that we inherit UserControl in our class declaration. You can have your user controls all use this class by changing the Control directive:



<%@ Control Language="c#" Inherits="BetterUserControl" %>


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.



Table 1.1. The Inheritance Sequence for Our User Control


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.



Another example of inheritance that is easier to implement and more relevant to stand-alone class design is to add a method to our Customer class. We might want to add a MailCustomer() method to the class, or perhaps we might want to override the Update() method to execute the base method and then email the customer.



/ 171