Developer .NET UPDATE—brought to you by the Windows & .NET Magazine Network
THIS ISSUE SPONSORED BY
Windows & .NET Magazine LIVE!
SQL Server Magazine LIVE!
(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.
August 13, 2002—In this issue:
1. DEVELOPER .NET PERSPECTIVES
- Play It Again, Sam
- Enter the Windows & .NET Magazine/Transcender Sweepstakes!
- The Backup and Recovery Solutions You've Been Searching For!
- Featured Thread: Database Programming in the Microsoft .NET Framework
4. NEW AND IMPROVED
- Generate Code
5. CONTACT US
- See this section for a list of ways to contact us.
1. DEVELOPER .NET PERSPECTIVES
(contributed by Bill Sheldon, [email protected])
Among software engineers, code reuse is a common practice. After engineers write a block of code that accomplishes a task, they don't want to have to write it again. One of the most reliable and effective ways to reuse code is through class inheritance. Unlike Visual Basic (VB), the Microsoft .NET Framework provides true inheritance. Because the .NET Framework is an object-based development environment, it provides not only interface inheritance (as in COM) but also implementation and source inheritance.
The .NET Framework offers class libraries that are based on common functions. Unfortunately, the Visual Studio .NET development team made working with classes much easier for developers who use Visual C# .NET. Visual C# .NET's syntax is simpler than that of the other .NET languages. Plus, Visual Studio .NET includes a class wizard for Visual C# .NET only. One can only speculate as to why Microsoft included just the C# Class Wizard in Visual Studio .NET. Maybe the class wizards for the other languages weren't finished in time for Visual Studio .NET's release, which prompted Microsoft to bury the C# Class Wizard in Visual Studio .NET.
If you create a Visual C# .NET class library project, the standard class opens within Visual Studio .NET's Solution Explorer. By switching from Solution Explorer to the Class View, you'll see your project's name followed by the default namespace. Right-clicking the project name in the Class View brings up a menu that contains an Add option. Selecting "Add a Class" on the Add option's submenu opens the C# Class Wizard. As I mentioned previously, Visual Studio .NET has a class wizard for Visual C# .NET only—if you create a Visual Basic .NET class library project and follow the same procedure, you won't get the same menu and submenu because they don't exist.
The C# Class Wizard has a wizard-like interface that gives you a helpful framework with which to create classes and implement inheritance. I say "wizard-like" because when the wizard's dialog box opens, it has a Finish button but not a Next button. Suppose you want to create a new class named Class2 to go with the default Class1 that Visual Studio .NET automatically created when you began this project. After you specify the new class's name, you can use the list of options on the right to move to the next screen. Select Base Class and not Inheritance. You use the Inheritance dialog box to implement COM-style interface inheritance, which I'll discuss in a future commentary. You use the Base Class option to specify the base class from which your object will inherit an implemented set of base properties and methods. Inheritance from parent classes is part of implementation inheritance, in which both the source methods and properties are available without needing to be reimplemented in your class.
The Base Class dialog box has only two edit fields, and both contain inaccurate information. The upper drop-down list displays the project's namespace. The lower drop-down list specifies the base class of the newly created class. By default, the wizard sets the base class to None. These settings are inaccurate because every .NET class inherits from System.Object and is part of the project namespace at a minimum. You can verify your object's inheritance by conducting the following exercise:
- Create a Windows forms project in Visual Basic .NET or Visual C# .NET and a reference to your sample library.
- Create an instance of your class, then call the ToString method. You'll see that the class and the method work. They work because unless you specify a different parent, every class inherits from System.Object. One of the beauties of the .NET Framework is that even if you select a different parent, that parent (or the class from which it inherits) winds up inheriting from System.Object at some point.
- Select a different parent in the class wizard by specifying Class1 as the base class for your new class, and click Finish.
In Visual Studio .NET, you now have a new class. When the wizard finishes, Visual Studio .NET displays the code in the Code Editor (also called the Text Editor). Looking at the code, you'll find the line
Public class Class2 : CSharpLibrarySample.Class1
This statement tells Visual C# .NET that this class has a parent of Class1. If you want to achieve this same inheritance in a Visual Basic .NET project, you would use a class declaration such as
Public Class Class2 Inherits VBLibrarySample.Class1
In Visual Basic .NET, you must put the Inherits keyword on a second line. If you try to put all the code on one line, Visual Basic .NET complains.
What has this inheritance code really accomplished? To find out, switch to the code for Class1 in the Code Editor. In the class's code, edit the Class1 definition and have it inherit from System.Random.
Having created this simple inheritance, you can now use a simple Windows form project to test your class. In the test project, create a variable called X as an instance of Class2. After you use the appropriate syntax to declare variable X, you can reference an object X method. Visual Studio .NET includes IntelliSense, a tool that displays language references. If you open IntelliSense's List Members option and type the letter X followed by a period, you'll get a list of all the methods and properties for that object. As this example shows, a class can have methods available, even if you haven't implemented any code for that class or its parent class.
If you can't see the IntelliSense list of functions, select Options on the Tools menu. In the Options dialog box, select Text Editor. For the language you're using, make sure that the Auto List Members check box is selected and that the Hide Advanced Members check box is cleared. These settings will ensure that IntelliSense displays the list of available functions.
The value of inheritance doesn't lie solely in the reuse of code. Inheritance also lets you manage complexity. For example, suppose you're creating an application that needs to access information in the registry. Instead of implementing System.Random, you can have a class that inherits from Microsoft.Win32.Registry. The Microsoft.Win32.Registry class can then act as a base class when you create other classes that need to access the registry. Because each new class would inherit from this base class, the functions to access the registry would be not only available but also already customized to read from the appropriate parent key and to handle errors in the same manner. Because you're using inheritance, you'll have only one copy of the registry-access code within the application. Every class that inherits from Microsoft.Win32.Registry reuses that code.
There's much more to inheritance than what I've covered here. I'll continue covering object-related topics in the coming weeks. I'll show you how to develop class libraries that let you reuse code and discuss some of the other features of .NET's object-oriented nature. In the meantime, start looking into inheritance. It's a powerful and important part of the .NET Framework.
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.
(brought to you by Windows & .NET Magazine and its partners)
Nothing can help you prepare for certification like Transcender products, and no one can help you master your job like Windows & .NET Magazine. Enter our combined sweepstakes contest, and you could win a Transcender Deluxe MCSE Select Pak (a $729 value) or one of several other great prizes. Sign up today!
Our popular Interactive Product Guides (IPGs) are online catalogs of the hottest vendor solutions around. Our latest IPG highlights the backup and recovery solutions and services that will help you recover your data and your network when disaster strikes. Download the IPG for free at:
Learn which resources colleagues recommend for learning about Visual Basic .NET and ASP.NET database programming. Go to
4. NEW AND IMPROVED
(contributed by Carolyn Mader, [email protected])
Codagen Technologies released Codagen Architect 2.5, add-on software that integrates with Microsoft Visio's Unified Modeling Language (UML) modeling tool and Visual Studio .NET. Codagen Architect can generate as much as 100 percent of the Visual Basic .NET or Visual C# .NET code associated with any application architecture and as much as 70 percent of the total application code. The add-on is part of the Codagen Development Suite, which also includes Codagen Generator and the Adapter. Pricing is based on a one-time perpetual license and an annual renewable license. Contact Codagen Technologies at 514-288-4802.
5. CONTACT US
Here's how to reach us with your comments and questions:
- ABOUT DEVELOPER .NET PERSPECTIVES — [email protected]
- ABOUT THE NEWSLETTER IN GENERAL — [email protected]
(please mention the newsletter name in the subject line)
- TECHNICAL QUESTIONS — http://www.winnetmag.net/forums
- PRODUCT NEWS — [email protected]
- QUESTIONS ABOUT YOUR DEVELOPER .NET UPDATE SUBSCRIPTION?
Customer Support — [email protected]
- WANT TO SPONSOR DEVELOPER .NET UPDATE?
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.
Receive the latest information about the Windows and .NET topics of your choice. Subscribe to our other FREE email newsletters.
Thank you for reading Developer .NET UPDATE.