When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles
Message Board
Related Web Technologies
User Tips!
Coding Tips

Sample Chapters
Commonly Asked Message Board Questions
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Stump the SQL Guru!
XML Info
Author an Article
ASP ASP.NET ASP FAQs Message Board Feedback
Print this page.
Published: Sunday, February 25, 2001

Using Object-Orientation in ASP.NET: Polymorphism, Part 2 - 4GuysFromRolla.com

By Ian Stallings

  • Read Part 1

  • In Part 1 we introduced the concept of polymorphism and looked at creating an ISaveData interface and having our Profile class inherit from this interface. Once Profile inherited from this interface, it could implement its own version of the .save() method. Since ExtendedProfile inherits from our Profile class, ExtendedProfile can also implement its own version of the .save() method. In this part we'll look at ExtendedProfile's implementation of this method and how to call this method for both classes from an ASP.NET page!

    - continued -

    Since the interface was implemented by ExtendedProfile's superclass, Profile, the IsaveData interface and it's method .save() is inherited. In this ExtendedProfile class I decided that I wanted to save the data into an XML file. I can share this XML file with other applications allowing them to access file. Later I can recreate the state of the object by reading the values from this file.

    public override void save() 
      String _document = 
      XmlTextWriter writer = null;
        writer = new XmlTextWriter (_document, null);
        writer.Formatting = Formatting.Indented;
        writer.WriteDocType("Profile", null, null, null);
          writer.WriteElementString("firstName", _firstName);
          writer.WriteElementString("lastName", _lastName);
          writer.WriteElementString("phoneNumber", _phoneNumber);
          writer.WriteElementString("address1", _address1);
          writer.WriteElementString("address2", _address2);
          writer.WriteElementString("city", _city);
          writer.WriteElementString("state", _state);
          writer.WriteElementString("postal", _postal);
       catch(Exception e)
        Console.WriteLine ("Exception: {0}", e.ToString());

    So above we have a situation where we used the same method name with different implementations using polymorphism. We can access these classes and test them using the following ASP.NET (.aspx) code:

    <%@ Import Namespace="Shai" %>
        font: 8pt verdana;
    <script language="C#" runat="server">
      public void Page_Load(Object sender, EventArgs E) {
        We create an instance of the superclass 
        below and save the data in binary format:
       Profile profile = new Profile();
       Message.InnerHtml += "<u>Profile Class</u><br>" +
                 "First: " + profile.getFirstName() + "<br>" +
                 "Last: " + profile.getLastName() + "<br>" +
                 "Phone: " + profile.getPhoneNumber() + "<br><br>";
        Then we create an instance of the subclass below 
        and save the data in xml format:
       ExtendedProfile extendedProfile = new ExtendedProfile();
       Message.InnerHtml += "<u>ExtendedProfile Class</u><br>" +
              "First: " + profile.getFirstName() + "<br>" +
              "Last: " + profile.getLastName() + "<br>" +
              "Phone: " + extendedProfile.getPhoneNumber() + "<br>" +
              "Adress1: " + extendedProfile.getAdress1() + "<br>" +
              "Adress2: " + extendedProfile.getAdress2() + "<br>" +
              "City: " + extendedProfile.getCity() + "<br>" +
              "State: " + extendedProfile.getState() + "<br>" +
              "Postal: " + extendedProfile.getPostal() + "<br>" +
              "Description: " + extendedProfile.getDescription()+"<br>";
    <body style="font: 10pt verdana">
      <b><h3>Simple Inheritance/Polymorphism Example</h3></b>
      Object Output:<br><br>
    	 <div id="Message" runat="server"/>

    And there you have it! This sample above will call both methods. One creating a .bin file, the other creating an .xml file. I have attached a zip file that contains the this article, the source code for the sample, the compiled dll, and instructions on how to compile and use the samples. You must have the .Net Framework SDK Beta1 installed on the machine in order to compile the samples and run them correctly. But even if you don't have the SDK installed (download the Beta1 SDK for free from www.ASP.NET) you can take a look at the sample source files and see polymorphism, OOP, and see how using .Net to build applications can make your life a little easier.

    Happy Programming!

  • By Ian Stallings


  • Download the source code (in ZIP format)
  • Visit the ASP.NET Article Index
  • Read Ian's Other ASP.NET OOP Articles

  • ASP.NET [1.x] [2.0] | ASPFAQs.com | Advertise | Feedback | Author an Article