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


THIS ISSUE SPONSORED BY

Microsoft ASP.NET Connections
http://lists.sqlmag.com/cgi-bin3/flo?y=eMrE0CCPlR0Bbp03cJ0Ap

Windows Developer Magazine
https://www.sdmediagroup.com/orders/orders.cgi?user_keycode=wdcdrom3
(below DEVELOPER .NET PERSPECTIVES)


SPONSOR: MICROSOFT ASP.NET CONNECTIONS

MICROSOFT ASP.NET CONNECTIONS TO CO-LOCATE
WITH SQL SERVER MAGAZINE LIVE!
Microsoft ASP.NET Connections and Visual Studio .NET Connections will co-locate with SQL Server Magazine LIVE! from Oct. 27-30. Register for one event and access sessions to the other two events for FREE. Save $2,990 when you register today, before your Early Bird discount expires. Access more than 170 sessions from top third-party gurus, Microsoft product developers, and magazine authors. This must-attend event will help you keep your skills sharp. Dig deeper into the Microsoft .NET Framework and discover how it will impact you and your company. Chock full of practical information you can use right away. Real-world technical tips and insights you don't want to miss. Register on the Web before this event sells out!
http://lists.sqlmag.com/cgi-bin3/flo?y=eMrE0CCPlR0Bbp03cJ0Ap


August 6, 2002—In this issue:

1. DEVELOPER .NET PERSPECTIVES

  • Of Constructors, Destructors, and the ToString

2. ANNOUNCEMENTS

  • Don't Miss Our Storage Web Seminar—Free!
  • Need to Keep Your Servers Running 24/7?

3. NEW AND IMPROVED

  • Consult a Technical Reference for Your .NET Questions

4. CONTACT US

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

1. DEVELOPER .NET PERSPECTIVES
(contributed by Bill Sheldon, bills@interknowlogy.com)

  • OF CONSTRUCTORS, DESTRUCTORS, AND THE TOSTRING

  • Last week, I introduced you to some primary concepts associated with OOP. This week, let's look at how to implement these concepts in the Microsoft .NET Framework and how to transition a set of Visual Basic (VB) 6.0 classes to Visual Basic .NET. Let's start with one of Visual Studio .NET's hidden treasures: the Visual Basic Upgrade Wizard.

    The Visual Basic Upgrade Wizard is a great feature of Visual Studio .NET that you won't find on any menu. To start the wizard, open Visual Studio .NET and select Open, Project on the File menu. In the Open Project dialog box that appears, select your existing VB 6.0 project file.

    The Visual Basic Upgrade Wizard converts your existing VB 6.0 project to Visual Basic .NET but does so without changing any existing project files. The wizard accomplishes this transformation by creating a new project in a separate directory—a project subdirectory, by default. A good resource about the transformation process is the Microsoft Developer Network (MSDN) Webcast "Best Practices for Upgrading VB 6.0 Projects to VB.NET." Microsoft is presenting this free Webcast on Friday, August 9, and Monday, September 16. For more information about this and other MSDN Webcasts, go to http://www.microsoft.com/usa/webcasts/upcoming.

    Running the Visual Basic Upgrade Wizard against a VB 6.0 class project results in a Visual Basic .NET class file that looks similar to the original, with the exception of some syntax changes. The exceptions include changes to the declaration of arrays and the replacement of any Let property methods with Get property methods. However, converted classes don't have the basic OOP features such as constructors and destructors that Visual Basic .NET offers.

    Constructors let you define an object's state during the instantiation (i.e., creation of an instance) of that object. By default, you don't have to explicitly declare a constructor for an object. However, you probably use constructors all the time but don't realize it. For example, in VB 6.0, every time you use the New keyword to create a class instance, you're implicitly calling a constructor.

    If you look at a Visual C# .NET class, you'll always find a method that has the same name as the class. For example, by default, the method

    public class MyCSPerson  \{
         public MyCSPerson()  \{
             // TODO: Add constructor logic here
          \}
    \}

    exists and is the constructor for the class. In Visual Basic .NET, you can have a constructor, but its syntax doesn't follow the syntax that's common to most other object-oriented (OO) languages. Unlike other OO language constructors, Visual Basic .NET constructors use the New keyword as the name of a Sub method. The Visual Basic .NET constructor syntax looks like

    Public Class MyVBPerson
          Sub New()
          End Sub
    End Class

    In VB 6.0, you can create a method with the same name as the class. However, if you attempt to create a Sub procedure named New, you'll receive an error. As a result, if you convert a VB 6.0 class to a Visual Basic .NET class, you'll find methods that match the class name, but none will have a predefined constructor method called New.

    As with other methods, you can overload or override a constructor but you can't have a constructor return a value. After you've used a constructor to initialize an object, you can use a second Sub New call to modify that object's state.

    Because constructors are powerful, most OO environments (especially those without garbage collectors) have destructors, which perform the opposite function of constructors. Destructors release all resources associated with an object, including those resources obtained during construction. When the underlying system is ready to dispose of an object, the system automatically executes that object's destructor.

    The .NET Framework includes a garbage collector, which means that, in most cases, destructors aren't necessary. However, most is not all, so the .NET Framework supports destructors.

    When you need to use a destructor in the .NET environment, you won't be using a method with that name. In the .NET Framework, Microsoft is moving away from the term "destructor" and instead using the term "finalize." Thus, in Visual Basic .NET, you use the Finalize method to dispose of an object. The syntax for Visual Basic .NET's Finalize method is

    Protected Overrides Sub Finalize()
    End Sub

    The Finalize method doesn't take any parameters. You can't overload the Finalize method, but you can override it.

    Visual C# .NET destructors follow the same format that other OO languages use: the name of the class preceded by a tilde (~). For example, the destructor for the Visual C# .NET class created earlier is

    public ~MyCSPerson()
    \{
    \}

    When you associate a destructor with a class in the .NET Framework, you're telling the .NET garbage collector that it can't simply delete your object. Instead, the .NET garbage collector has to mark the instance of this class, then execute the destructor before removing the object from memory. Although in some languages creating default destructors that do nothing is common practice, you should avoid this practice in the .NET environment. Destructors interfere with the garbage collector. Unlike constructors, destructors aren't created by default for each new class.

    As I discussed last week, every object in the .NET Framework has a base class. For most objects, this class is the default Object class. No matter whether you create a new class or run an existing class through the Visual Basic Upgrade Wizard, the class definition doesn't mention the default base class.

    The idea that every class supports a common set of methods is based on the use of inheritance. Definitions for these common, or default, methods are available because they're defined in the parent class. For example, one of the default methods in the Object class is the ToString method. This method doesn't have anything to do with the creation of your object, and odds are that you won't take the time to write a custom implementation. Yet whether you're working in Visual C# .NET or Visual Basic .NET, you'll find that this method is available on your object. If you run the code

    MyObject.ToString()

    you'll obtain the same results whether you use Visual C# .NET or Visual Basic .NET to declare the object. Because this method is part of the Object class—which is ultimately the base class for every object—you can always rely on this method's availability.

    Next week, I'll discuss base classes and inheritance in more detail. Until then, if you want to find out more information about the .NET Framework constructors and destructors, go to http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcn7/html/vaconFinalizeDestructors.asp.


    SPONSOR: WINDOWS DEVELOPER MAGAZINE

    WINDOWS DEVELOPER MAGAZINE CD-ROM
    VERSION 5.0 ON SALE NOW FOR ONLY $29.95!
    Windows Developer Magazine's CD-ROM 5 gives you all editorial content and source code from December 1991 through June 2002 issues. For more than 10 years, Windows Developer Magazine (formerly Windows Developer's Journal) has been the independent technical magazine for Windows programmers covering articles ranging from quick technical tips to ready to use solutions for complex programming problems. Turn to Windows Developer as a unique and trusted source of Windows to Web development solutions. Offer ends September 30, 2002.
    https://www.sdmediagroup.com/orders/orders.cgi?user_keycode=wdcdrom3


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

  • DON'T MISS OUR STORAGE WEB SEMINAR—FREE!

  • While the cost of buying storage capacity continues to drop, the cost of managing storage and keeping it available continues to rise. Find out why this happens and how to address it by bringing your Windows storage under control. Register today for this important online seminar sponsored by VERITAS!
    http://www.winnetmag.com/seminars/veritas

  • NEED TO KEEP YOUR SERVERS RUNNING 24/7?

  • Join Morris Lewis for our next Web Seminar, "Planning Highly Available Database Server Environments," on August 27. This seminar will explain methods for achieving high availability and detail the criteria you must evaluate to determine which options will best suit your tolerance for risk and your budget. Register today!
    http://www.sqlmag.com/sub.cfm?code=mlei322hmu

    3. NEW AND IMPROVED
    (contributed by Carolyn Mader, products@winnetmag.com)

  • CONSULT A TECHNICAL REFERENCE FOR YOUR .NET QUESTIONS

  • O'Reilly released Ted Neward and Budi Kurniawan’s "VB.NET Core Classes in a Nutshell," a technical reference for programmers who are working through everyday development tasks. The book contains detailed descriptions of more than 700 types found in 22 core Microsoft .NET namespaces. Each entry describes a type's significance, explains how you can use it in .NET applications, and lists the type's members and their signatures in readable Visual Basic .NET syntax. The book targets programmers familiar with previous versions of Visual Basic (VB) who haven't explored Visual Basic .NET. Pricing is $44.95. Contact O'Reilly at 617-354-5800 or 800-775-7731.
    http://www.oreilly.com

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

    • ABOUT DEVELOPER .NET PERSPECTIVES — bills@interknowlogy.com
    • ABOUT THE NEWSLETTER IN GENERAL — kbemowski@winnetmag.com

    (please mention the newsletter name in the subject line)

    • TECHNICAL QUESTIONS — http://www.winnetmag.net/forums
    • PRODUCT NEWS — products@winnetmag.com
    • QUESTIONS ABOUT YOUR DEVELOPER .NET UPDATE SUBSCRIPTION?
      Customer Support — Developer_dot_NET_update@winnetmag.com
    • WANT TO SPONSOR DEVELOPER .NET UPDATE?
      emedia_opps@winnetmag.com

    This weekly 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

    Thank you for reading Developer .NET UPDATE.