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

Sections:
Sample Chapters
Commonly Asked Message Board Questions
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Security
Stump the SQL Guru!
XML Info
Information:
Feedback
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 = 
         "c:\\inetpub\\wwwroot\\asp.net\\polymorphism\\newbooks.xml";
      XmlTextWriter writer = null;
    		
      try
      {
        writer = new XmlTextWriter (_document, null);
    
        writer.Formatting = Formatting.Indented;
        writer.WriteStartDocument(false);
        writer.WriteDocType("Profile", null, null, null);
        writer.WriteStartElement("Profile");
          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);
        writer.WriteEndElement();
        writer.Flush();
        writer.Close();
      }
       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" %>
    <html>
    <style>
      div 
      { 
        font: 8pt verdana;
        background-color:cccccc;
        border-color:black;
        border-width:1;
        border-style:solid;
        padding:10,10,10,10; 
      }
    </style>
    
    <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>";
    
       profile.save();
        	
       /*
        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>";
    
       extendedProfile.save();
    }
    </script>
    
    <body style="font: 10pt verdana">
      <b><h3>Simple Inheritance/Polymorphism Example</h3></b>
      <br><br>	
      Object Output:<br><br>
    	 <div id="Message" runat="server"/>
    </body>
    </html>
    

    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


    Attachments:

  • 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] | ASPMessageboard.com | ASPFAQs.com | Advertise | Feedback | Author an Article