Skip navigation

Developer .NET UPDATE, August 27, 2002

Developer .NET UPDATE—brought to you by the Windows & .NET Magazine Network
http://www.winnetmag.com


THIS ISSUE SPONSORED BY

Windows & .NET Magazine LIVE!
http://www.winnetmagLIVE.com

SQL Server Magazine LIVE!
http://www.sqlmagLIVE.com
(below DEVELOPER .NET PERSPECTIVES)


SPONSOR: WINDOWS & .NET MAGAZINE LIVE!

REAL-WORLD TIPS AND SOLUTIONS HERE FOR YOU
This conference is chock full of "been there, done that" information from people who use Microsoft products in the real world. Increase your productivity with shortcuts, tips, and tricks you'll learn only at Windows & .NET Magazine LIVE! (Oct 30. Nov. 2).

  • Solve those tough interoperability issues.
  • Enhance and automate systems administration with new tools.
  • Learn how Microsoft .NET will impact your job.
  • This must-attend event will help you keep your skills sharp. Loaded with practical information you can use right away. Real-world technical tips and insights you don't want to miss.

    Register now and you'll receive FREE access to sessions of concurrently run XML Web Services Connections. Stay the whole week and catch SQL Server Magazine LIVE! and Microsoft ASP.NET Connections at deeply discounted prices. Register today before these events sell out.
    https://secure.win2000mag.com/events/windows_register.asp

    August 27, 2002—In this issue:

    1. DEVELOPER .NET PERSPECTIVES

    • Overriding and Shadowing Classes

    2. ANNOUNCEMENTS

    • Planning on Getting Certified? Make Sure to Pick Up Our New eBook!
    • Mobile and Wireless Solutions—An Online Resource for a New Era

    3. RESOURCE

    • Featured Thread: Problems with Using ASP.Net to Connect to a Database

    4. NEW AND IMPROVED

    • Give Developers Direct IDE Access

    5. CONTACT US

    • See this section for a list of ways to contact us.

    1. DEVELOPER .NET PERSPECTIVES
    (contributed by Bill Sheldon, [email protected])

  • OVERRIDING AND SHADOWING CLASSES

  • This week, let's take a look at two concepts associated with implementing methods and properties in a class hierarchy: overriding and shadowing. To understand the difference between overriding and shadowing, you need to look at the keywords associated with the concepts.

    Let's start by looking at Visual Basic .NET's Overridable keyword, which is associated with the concept of overriding. (The equivalent keyword in Visual C# .NET is Virtual.) When you initially define a base class, you can declare that class's methods and properties as Overridable. Adding the Overridable keyword to methods and properties doesn't overtly change how the base class works. Instead, adding this keyword specifies that a child class can—but doesn't necessarily have to—override them. For example, building on the person, employee, engineer, and author classes I discussed last week, suppose that you assign the AssignTask method to the person class and declare that method as Overridable. If you define the employee class as a child of the person class, the employee class still has access to the AssignTask method, even though you defined the method as Overridable.

    All the Overridable keyword does is tell the OS that when it encounters a method flagged as Overridable, it can't just run the base class's method (aka the base method). Instead, the OS needs to determine whether the underlying object is based on a class derived from the current class. If the object is a child class, the OS checks the child class's definition to see whether a custom version of the method exists. When the OS finds a custom method, it always runs that method.

    Coupled with the Overridable keyword is the Overrides keyword. (The equivalent keyword in Visual C# .NET is Override.) You use the Overrides keyword in the child class's definition to flag a custom method or property. For example, suppose you want to override the AssignTask method in the employee class called MyEmpl. By declaring

    Overrides AssignTask

    in MyEmpl class definition, the compiler knows to call the method defined in the employee class rather than the person class. A call to the MyEmpl.AssignTask method retrieves the custom method for the employee class. This retrieval is probably what you expect the OS to do. However, thanks to polymorphism, the OS also knows to call the custom method, even when the object has been cast as its base class. Thus, you can create an employee object, then pass it to a function as an instance of a person object. If you call AssignTask method within that function, the OS calls the employee-class version of AssignTask because the OS knows that this person object is actually an instance of an employee object.

    For this reason, you flag methods as Overridable in the base class. The flag in the base class tells the OS at runtime to look for a custom method. Inheritance and casting to a base class is a significant change from how traditional languages cast items. In traditional languages, the OS manipulates the underlying data to fit the type to which a variable is cast. In an object-oriented (OO) system, the underlying data doesn't change. Instead, the OS manages access to those methods and properties that are appropriate for the class to which the variable is cast.

    For a custom method to override the base method, the custom method's name, parameter list, and return type must match those of the base method. Because you define an overridden method at the base-class level, the class hosting the method must be familiar with all the input and output data types. Thus, although overriding is powerful, it has limitations. If you want to return a complex type that's related to the child class instead of returning a base type, you need to use shadowing.

    To illustrate shadowing, suppose that the author class inherits from the person class, but the AssignTask method for the author class needs to have a return type of ArticleAssignment instead of the inherited return type. Although the code that works with the author class knows what an ArticleAssignment return type is, the person class has no concept of ArticleAssignment because the AssignTask method operates in an environment that's limited to objects common to the base class. Thus, if you attempt to override the AssignTask method with a return type of ArticleAssignment, a type-safety problem would occur. Instead of being certain of the return type for AssignTask at compile time, the method call might return a different type, which isn't good programming practice.

    Instead of overriding, you need to replace AssignTask with a different implementation. In Visual Basic .NET, you use the Shadows keyword in the child class's definition to specify a replacement. (The equivalent keyword in Visual C# .NET is New.) Unlike the Overrides and Overridable keyword pairing, Shadows doesn't have a companion keyword in the base class. Because the Shadows keyword applies only to the child class, that class can replace a base method (overridable or not).

    To better illustrate the difference between overriding and shadowing, I've included some incomplete declarations in Visual Basic .NET and Visual C# .NET at the end of this column. An important concept to remember is that the Overrides keyword depends on the base class knowing through the use of an Overridable or Virtual keyword declaration that a method might have a custom implementation. The Shadows keyword provides a way for you to customize the implementation of a method in a child class, but that customization isn't available when the object is cast to the base class.

    Overridable, Overrides, and Shadows aren't the only keywords you can use to implement different methods and properties in a child class. For more information about overriding and shadowing, go to these respective URLs:
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcn7/html/vaconinheritancebasics.asp
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbls7/html/vblrfvbspec4_2_3.asp

    Visual Basic .NET Sample Code
    
    Public class Employee
       Public Overridable Function AssignTask(string MyTask) as String
          //Add logic
       End Function
    End Class
    
    Public class Employee
       Inherits VBLibrarySample.Person
       Public Overrides Function AssignTask(string MyTask) as String
          //Add Logic
          return MyTask
       End Function
    End Class
    Public class Employee
       Inherits VBLibrarySample.Person
       Public Shadows Function AssignTask(string MyTask) as ArticleAssignment
          //Add Logic
          return MyArticle
       End Function
    End Class
    
    Visual C# .NET Sample Code
    
    public class Person
    \{
       public virtual string AssignTask(string MyTask)
       \{
          //Add logic
       \}
    \}
    public class Employee : CSharpLibrarySample.Person
    \{
       public override string AssignTask(string MyTask)
    \{
       //Add Logic
       return MyTask;
    \}
    \}
    public class Author:CSharpLibrarySample.Person
    \{
       public new ArticleAssignment AssignTask(string MyTask)
       \{
          //Add Logic
          return MyArticle;
       \}
    \}
    

    SPONSOR: SQL SERVER MAGAZINE LIVE

    NEW IDEAS, TIPS, AND ADVICE YOU NEED TO KNOW
    Over 60 SQL-specific sessions in this real-world, best-practices-packed conference. Ready for your consumption are the latest SQL tools, tips, and real-life examples you can't live without.

  • Discover the best ways to integrate SQL with web applications.
  • Enhance database development and administration with new tools.
  • Learn the newest technology insights from Microsoft product architects.
  • Increase your productivity with shortcuts, tips, and tricks you'll only learn here.
  • Register now and access concurrently run Microsoft ASP.NET Connections and Visual Studio .NET Connections for FREE—over 160 sessions to choose from.
    https://secure.win2000mag.com/events/sql_register.asp


    2. ANNOUNCEMENTS
    (brought to you by Windows & .NET Magazine and its partners)

  • PLANNING ON GETTING CERTIFIED? MAKE SURE TO PICK UP OUR NEW EBOOK!

  • "The Insider's Guide to IT Certification" eBook is hot off the press and contains everything you need to know to help you save time and money while preparing for certification exams from Microsoft, Cisco Systems, and CompTIA and have a successful career in IT. Get your copy of the Insider's Guide today!
    http://winnet.bookaisle.com/ebookcover.asp?ebookid=13475

  • MOBILE AND WIRELESS SOLUTIONS—AN ONLINE RESOURCE FOR A NEW ERA

  • Our mobile and wireless computing site has it all—technical articles, product reviews, forums, and other resources to help you support a wireless network and mobile users. Check it out today!
    http://www.mobile-and-wireless.com

    3. RESOURCE

  • FEATURED THREAD: PROBLEMS WITH USING ASP.NET TO CONNECT TO A DATABASE

  • A developer is having problems with an ASP.NET Web application in Visual Studio .NET. When he tries to connect to a Microsoft SQL Server 6.5 database on another machine, he receives the error message "SQL Server does not exist or access denied." See this thread
    http://www.sqlmag.com/forums/messageview.cfm?catid=8&threadid=8220

    4. NEW AND IMPROVED
    (contributed by Carolyn Mader, [email protected])

  • GIVE DEVELOPERS DIRECT IDE ACCESS

  • MKS announced the integration of MKS Source Integrity Enterprise Edition and Visual Studio .NET. The integration gives developers direct IDE access to advanced configuration-management features, such as create project, create Sandbox environments, file merge and compare, and file check in and out. Integration with Visual Studio .NET is available with MKS Source Integrity Enterprise Edition 8.2a. Existing customers can receive the patch from MKS customer support. Contact MKS at 800-265-2797.
    http://www.mks.com

    5. CONTACT US
    Here's how to reach us with your comments and questions:

    This email newsletter is brought to you by Windows & .NET Magazine, the leading publication for Windows professionals who want to learn more and perform better. Subscribe today.
    http://www.winnetmag.com/sub.cfm?code=wswi201x1z

    Receive the latest information about the Windows and .NET topics of your choice. Subscribe to our other FREE email newsletters.
    http://www.winnetmag.net/email

    Hide comments

    Comments

    • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

    Plain text

    • No HTML tags allowed.
    • Web page addresses and e-mail addresses turn into links automatically.
    • Lines and paragraphs break automatically.
    Publish