Skip navigation

Developer .NET UPDATE, August 20, 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 20, 2002—In this issue:

    1. DEVELOPER .NET PERSPECTIVES

    • Speaking as an Engineer and an Author

    2. ANNOUNCEMENTS

    • Planning on Getting Certified? Make Sure to Pick Up Our New eBook!
    • Take Our Survey and You Could Win a Free T-Shirt!

    3. RESOURCE

    • Featured Thread: Visual Studio .NET Mystery

    4. NEW AND IMPROVED

    • Understand How Your Applications Interact with Oracle

    5. CONTACT US

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

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

  • SPEAKING AS AN ENGINEER AND AN AUTHOR

  • I work as an engineer for InterKnowlogy and as an author for Penton Technology Media. These two roles have a few tasks in common (e.g., writing code), yet how I accomplish the common tasks differs for each role.

    Just as I have two roles, objects in an application can have two (or more) roles. To fill the dual roles, the object might need to inherit methods and properties from two classes. This concept is known as multiple inheritance. Languages that support multiple inheritance include C++ and SmallTalk.

    Problems can arise when an object inherits from two classes and those classes have a method or property with the same name but not the same functionality. When the runtime environment encounters such a method, it determines which one of the two methods to use based on the declaration order of the inherited classes within the source file. In other words, the runtime environment checks an inheritance hierarchy and defaults to whichever class is highest in the hierarchy.

    Selecting a method based on an inheritance hierarchy isn't the most type-safe way to handle ambiguous situations. At compile time, type safety ensures that the code specifies the correct underlying data and method format so that you can optimize the code and easily detect and correct any errors. The problem with multiple inheritance is that while the compiler might select a method, its selection isn't necessarily apparent or correct. For this reason, the Microsoft .NET Framework doesn't support multiple inheritance, which means every .NET class has only one parent. Not supporting multiple inheritance ensures type safety because only one set of methods and properties exists for each class. However, the lack of support for multiple inheritance makes handling an object with multiple roles quite challenging. To resolve this problem, the .NET Framework supports interface inheritance, which provides the core features of multiple inheritance without leaving any runtime ambiguity regarding which method to execute.

    To understand how interface inheritance works, suppose that when I was first hired as an engineer, I created an application that included an object that represented my role as an engineer. At the time, I didn't know I would later fill the role of an author, so I created an engineer class that inherits from an employee class, which in turn, inherits from a person class.

    This class structure works quite well for my initial role and lets the application handle different types of employee roles should I decide to change jobs within the company. However, I now need to add my new role as an author to this class definition. Having the author class inherit from an existing class in the class structure might be tempting, but doing so would incorrectly associate authors with a specific role within the hierarchy. Not all engineers are authors, and not all authors are engineers. Thus, I need to implement a second role that doesn't inherit from the same structure that the existing object uses. In addition, I don't want to have to change and retest the implementation of the engineer class to account for this additional role. In this type of situation, interface inheritance comes in handy. (For an overview about when you should use interfaces, go to http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcn7/html/vaconwhentouseinterfaces.asp.)

    The Interface keyword in both Visual Basic .NET and Visual C# .NET lets you define an interface. Fortunately, an interface's definition is similar in both languages. You specify the protection level (typically public), name of the interface, and any methods or properties that are required to define an object of that type. For example, if you want to declare and define an interface called IAuthorProp in Visual Basic .NET, you use the code

    Public Interface IAuthorProp
       Property MyTask() as String
       String AssignArticle(string MyTask);
    End Interface

    The IAuthorProp interface defines one property called MyTask, which is a String type. The interface also defines a method called AssignArticle that you can use to assign a task to an author. However, the interface doesn't define what happens when you assign an article to an author—that definition depends on the class that implements the interface.

    Let's define a similar interface in Visual C# .NET. To declare and define an interface called IAuthorMeth that has one mandatory method called AssignTask, you use the code

    public interface IAuthorMeth
    \{
       String AssignArticle(string MyTask);
    \}

    In both the Visual Basic .NET and Visual C# .NET sample code, notice that the declarations don't include implementation code. Unlike implementation and source inheritance, interface inheritance doesn't provide for implementation. In interface inheritance, the class handles the implementation. Thus, each class that implements the interface can provide its own unique definition. The implementation won't be reused unless another class inherits it.

    To prompt an interface's implementation in Visual Basic .NET, you use the Implements keyword. You must use the Implements keyword not only in the class definition but also in the definition of each method and property. For example, suppose you want to implement an interface called IAuthorCS that has the AssignArticle method and the MyTask property. In the class definition, you use the code

    Public Class Engineer
       Inherits Employee
       Implements IAuthorCS

    In the definition of the AssignArticle method, you use the code

    Public Sub AssignArticle( ByVal MyTask as String) _
       Implements IAuthorCS.AssignArticle
       ' code
    End Sub

    In the definition of the MyTask property, you use the code

    Public Property MyTask() As String Implements IAuthorsCS.MyTask
       Get
          Return MyTask
       End Get
       Set(ByVal Value As String)
          MyTask = Value
       End Set
    End Property

    For more information about defining interfaces in Visual Basic .NET, go to http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcn7/html/vaconinterfaces.asp.

    Like Visual Basic .NET, Visual C# .NET requires you to include implementation code in the class definition and in each property's or method's definition. However, you don't use the Implements keyword in Visual C# .NET. Instead, in the class definition, you place the interface to be implemented after the base class. Thus, in Visual C# .NET, the class definition for the IAuthorCS interface would be

    public class Engineer : Employee, IAuthorCS

    Then, to implement the AssignArticle method and MyTask property, you use the code

    public void AssignArticle(string MyTask)
    \{
       // code
    \}
    
    public string MyTask
    \{
       get
       \{
          return MyTask;
       \}
       set 
       \{
          MyTask = value;
       \}
    \}

    The AssignArticle method looks and behaves like any other method in the class. The compiler automatically recognizes that the AssignArticle method is part of the interface and not the class. For more information about defining interfaces in Visual C# .NET, see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cscon/html/vcurfccodewizards.asp.

    Whether you're working with Visual Basic .NET or Visual C# .NET, interface implementation lets you extend the functionality that's part of the class definition without placing other code that relies on that definition at risk. Thus, after you implement the interface, you can cast the class as a type, with code such as

    Dim Eng as New Engineer
    Dim Aut as IAuthorCS = Eng

    Even better than casting is creating code in the application that accepts a parameter defined as IAuthorCS. Thus, whether the object with which you're working represents an engineer or a project manager, you can pass this object to the code as long as the object being passed includes the implementation of the IAuthorCS interface. Therein lies the power of interfaces—they let you extend the class structure without requiring you to change the inheritance structure.

    One disadvantage in how the .NET Framework implements interfaces involves the overlap of the same methods. If two interfaces define the same method or property, the class implementing them needs to create a single implementation. In Visual C# .NET, the creation of a single implementation occurs automatically because, by definition, you can have two different method implementations with the same name and parameter list. In Visual Basic .NET, you can't have two different method implementations with the same name and parameter list, so you need to extend the Implements line. For example, if both the IAuthorCS and IAuthorVB interfaces defined the AssignArticle Method, the class, method, and property definitions would be

    Public Class Engineer
       Inherits Employee
       Implements IAuthorCS
       Implements IAuthorVB
    
       Public Sub AssignArticle( ByVal MyTask as String) _
          Implements IAuthorCS.AssignArticle, IAuthorVB.AssignArticle
    
          ' code
       End Sub
       Public Property MyTask() As String _
          Implements IAuthorsCS.MyTask, IAuthorsVB.MyTask
             Get
                Return MyTask
             End Get
             Set (ByVal Value As String)
                MyTask = Value
             End Set
       End Property
    End Class

    Thus, when you're creating interface definitions, you'll want to assign unique names to those methods and properties that require specific implementations. This point leads us to another disadvantage of the .NET Framework: It doesn't provide a clean way to support different actions based on the use of, for example, the IAuthorMeth.AssignArticle method vs. the IAuthorProp.AssignArticle method. Instead, you must make sure that these methods have different implementations by either assigning the methods different names or by adding parameters to one method so that the methods have different parameter lists.

    Although the .NET environment doesn't support multiple inheritance, it uses interface inheritance to provide the same power. At the same time, the .NET Framework prevents ambiguous inheritance, thereby ensuring type safety during compilation.


    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

  • TAKE OUR SURVEY AND YOU COULD WIN A FREE T-SHIRT!

  • We need to hear your thoughts on the future of technology! Take our reader survey, and you'll be entered to win a T-shirt, compliments of Windows & .NET Magazine. All responses are completely confidential, so visit
    http://www.up2research.com/windotnet

    3. RESOURCE

  • FEATURED THREAD: VISUAL STUDIO .NET MYSTERY

  • Kerry is able to use Visual Studio .NET on one computer but not another in the same lab, despite logging on to each computer as the same user with the same permissions. If you can shed some light on this mystery, go to
    http://www.winnetmag.com/forums/messageview.cfm?catid=36&threadid=44634

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

  • UNDERSTAND HOW YOUR APPLICATIONS INTERACT WITH ORACLE

  • Compuware announced DevPartnerDB for Oracle, software that can debug Oracle stored procedures from within the Visual Studio .NET development environment. DevPartnerDB for Oracle is a development tool for performing proactive analysis and debugging of Procedural Language/SQL (PL/SQL). The software helps developers understand how their applications interact with Oracle. DevPartnerDB for Oracle runs on Windows XP and Windows 2000 systems and supports Oracle 8.0 and higher. For pricing, contact Compuware at 248-737-7300.
    http://www.compuware.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