03.11.2016 Views

Beginning ASP.NET 4.5 in CSharp and VB Opsylum

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

198 x CHAPTER 5 PROGRAMMING YOUR <strong>ASP</strong>.<strong>NET</strong> WEB PAGES<br />

Figure 5-7 shows that Person <strong>in</strong>herits from Object (<strong>in</strong>dicated by the arrow po<strong>in</strong>t<strong>in</strong>g <strong>in</strong> the direction<br />

of the class that is be<strong>in</strong>g <strong>in</strong>herited from), which <strong>in</strong> turn means that a Person <strong>in</strong>stance can do whatever<br />

an Object can do. So, for example, you can call ToStr<strong>in</strong>g() on your Person object:<br />

Label1.Text = myPerson.ToStr<strong>in</strong>g()<br />

' Writes out Person<br />

The default behavior of the ToStr<strong>in</strong>g() method def<strong>in</strong>ed <strong>in</strong> Object is to say its own class name. In<br />

the preced<strong>in</strong>g example, it means that the Person class <strong>in</strong>herits this behavior <strong>and</strong> thus says Person<br />

as its name. Usually, this default behavior is not enough, <strong>and</strong> it would be much more useful if the<br />

Person could return the full name of the person it is represent<strong>in</strong>g, for example. You can easily do<br />

this by overrid<strong>in</strong>g the ToStr<strong>in</strong>g() method. Overrid<strong>in</strong>g a method or property redef<strong>in</strong>es the behavior<br />

the class <strong>in</strong>herits from its parent class. To override a method you use the keyword Overrides<br />

<strong>in</strong> <strong>VB</strong>.<strong>NET</strong> <strong>and</strong> override <strong>in</strong> C#. The follow<strong>in</strong>g snippet redef<strong>in</strong>es the behavior of ToStr<strong>in</strong>g <strong>in</strong> the<br />

Person class:<br />

<strong>VB</strong>.<strong>NET</strong><br />

Public Overrides Function ToStr<strong>in</strong>g() As Str<strong>in</strong>g<br />

Return FullName & ", born at " & _dateOfBirth.ToShortDateStr<strong>in</strong>g()<br />

End Function<br />

C#<br />

public override str<strong>in</strong>g ToStr<strong>in</strong>g()<br />

{<br />

return FullName + ", born at " + _dateOfBirth.ToShortDateStr<strong>in</strong>g();<br />

}<br />

With this def<strong>in</strong>ition of ToStr<strong>in</strong>g <strong>in</strong> the Person class, it no longer returns the word Person, but now<br />

returns the full name of the person it is represent<strong>in</strong>g:<br />

Label1.Text = myPerson.ToStr<strong>in</strong>g() ' Imar Spaanjaars, born at 8/9/1971<br />

Notice how the code uses the read-only FullName property to avoid cod<strong>in</strong>g the logic of concatenat<strong>in</strong>g<br />

the two names aga<strong>in</strong>. You can’t just override any method member you want to. For a method<br />

to be overridable, the parent class needs to mark the member with the keyword virtual (<strong>in</strong> C#) or<br />

Overridable (<strong>in</strong> <strong>VB</strong>.<strong>NET</strong>).<br />

Object <strong>in</strong>heritance <strong>in</strong> .<strong>NET</strong> enables you to create a hierarchy of objects that enhance, or add functionality<br />

to, other objects. This enables you to start out with a generic base class (Object). Other<br />

classes can then <strong>in</strong>herit from this class, add<strong>in</strong>g specialized behavior. If you need even more specialized<br />

classes, you can <strong>in</strong>herit aga<strong>in</strong> from the class that <strong>in</strong>herits from Object, thus creat<strong>in</strong>g a hierarchy<br />

of classes that keep gett<strong>in</strong>g more specialized. This pr<strong>in</strong>ciple works for many classes <strong>in</strong> the .<strong>NET</strong><br />

Framework, <strong>in</strong>clud<strong>in</strong>g the Page class. You may not realize it, but every <strong>ASP</strong>X page you create <strong>in</strong> VS<br />

is actually a class that <strong>in</strong>herits from the class System.Web.UI.Page. This Page class <strong>in</strong> turn <strong>in</strong>herits<br />

from TemplateControl, which <strong>in</strong>herits from Control, which <strong>in</strong>herits from Object. The entire<br />

hierarchy is shown <strong>in</strong> Figure 5-8. At the bottom you see the class MyWebPage, which could be a Code<br />

Beh<strong>in</strong>d class of a page such as MyWebPage.aspx.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!