Developer .NET UPDATE—brought to you by Windows & .NET Magazine Network.
THIS ISSUE SPONSORED BY
Connections .NET Tour
SQL Server Magazine LIVE!
(below DEVELOPER .NET PERSPECTIVES)
SPONSOR: CONNECTIONS .NET TOUR
TRAVEL BUDGET CUT? SOLUTION = CONNECTIONS .NET TOUR
Connections .NET Tour means we are bringing our events to you. Microsoft ASP.NET Connections, VB.NET Connections, and C# Connections will hit the road.
September 23 & 24
September 30 & October 1
Redmond, WA (Day 1 only on Microsoft's Campus)
Three in-depth tracks covering ASP.NET, Visual Basic .NET, and Visual C# .NET. Stay in one track all day or float from session to session across tracks. Attend Day 1 or Day 2 as single seminars or maximize your learning and Q&A time by attending both days.
Don't let a lean travel budget prevent you from keeping your competitive edge as a developer. Reserve your seat today by calling 800-438-6720 or visit
September 3, 2002—In this issue:
1. DEVELOPER .NET PERSPECTIVES
- Method Overloading
- Take Our Exchange Survey and Enter to Win a Microsoft Xbox!
- Mobile and Wireless Solutions--An Online Resource for a New Era
- Featured Thread: Problem with .msi Files on Machine Running Visual Studio .NET
4. NEW AND IMPROVED
- Connect to Enterprise Data
5. CONTACT US
- See this section for a list of ways to contact us.
1. DEVELOPER .NET PERSPECTIVES
(contributed by Bill Sheldon, email@example.com)
This week, I want to talk about the object-oriented (OO) concept of method overloading. When method overloading is present in OO languages, it replaces the use of optional parameters. The Microsoft .NET Framework provides method overloading in its .NET languages. Overloading a method in Visual Basic .NET and Visual C# .NET is easy as long as you're aware of a limitation and some caveats.
Some languages, such as Visual Basic (VB) 6.0 and C, let you declare methods that have optional parameters. The disadvantage of optional parameters is that you must constantly account for them. For example, in VB 6.0, suppose you want to call a method that has three optional parameters. If you want to use only the third optional parameter, you must pass in two placeholder commas before the third parameter's value. In C, you must place all optional parameters at the end of a method call and you must pass in a value for every parameter. Thus, if a function has three optional parameters and you want to use only the third parameter, you need to pass in "null" for the first two parameters followed by the value for the third parameter.
If you initially created the class to which the method belongs, you can include placeholder commas or null values fairly easily. However, if you didn't write the class, how do you know that the combination of parameters you're passing in to the method call will work? For all you know, to use the third parameter, you might need to also pass in a fourth parameter. Problems can also arise if you need to modify the class so that the method accepts different or new parameters. The bottom line is that you don't have a good way to maintain the logic necessary to use optional parameters.
An alternative to using optional parameters is method overloading. In method overloading, all the parameters are mandatory; however, when you create an instance of the class object, you can declare different versions of the same method call. Each version can have different parameters. So, instead of declaring a single method that has three optional parameters, you can declare the method three times, specifying a different set of parameters each time.
The basic idea behind overloading is that you can create multiple versions of a method. You just need to meet the following two criteria for each version:
Meeting these two criteria for overloading is fairly straightforward and, to the best of my knowledge, poses only one minor limitation compared with optional parameters. This limitation involves the situation in which a method accepts several of the same type of parameters. For example, suppose you have a VB 6.0 method that accepts two strings: a message and window title. To declare this method in VB 6.0, you might use code such as
Optional strTitle As String = "Warning")
With optional parameters, you can call this function four ways: without passing any parameters, passing only a message, passing only a title, or passing both a message and title. However, with method overloading, you can simulate only three of these four options. You can create a version that accepts no parameters, a version that accepts one parameter (either the message or the title), and a version that accepts two parameters (both the message and title). You can't create a version that accepts the message parameter and another version that accepts the title parameter. The method declaration would fail because the number of parameters doesn't vary and the parameters are the same type (i.e., strings) in these two versions.
In those cases in which you can't simulate all the options, you can define alternative methods instead of defining alternative versions of the same method. For example, for the preceding VB 6.0 declaration, you can use the following Visual Basic .NET declarations:
Public Function Message(ByVal strMsg As String) As String
Public Function Message(ByVal strMsg As String,
ByVal strTitle As String) As String
Public Function MessageTitle(ByVal strTitle As String) As String
In these declarations, notice the absence of a keyword. Although Visual Basic .NET includes a keyword (i.e., Overloads), you don't need to use this keyword to overload a method in Visual Basic .NET. (Visual C# .NET doesn't have a keyword for overloading.) In fact, I highly recommend that you pretend the Overloads keyword doesn't exist because using it causes problems. After you label one instance of an overloaded method with the Overloads keyword, you must label all the other instances with this keyword. Forgetting to do so will cause the method declaration to fail. In addition, if you use the Overloads keyword, you can't use the Shadows keyword in the same method declaration. (When two methods share the same name, one can hide, or "shadow" the other.) I'm not saying that you can't overload a method that uses the Shadows keyword. You just can't combine the Shadows keyword and Overloads keyword in the same method declaration.
Method overloading is a powerful alternative to optional parameters because it provides almost all the adaptability of optional parameters, much greater control over which parameter combinations are valid, and easily maintainable code. For more information about method overloading in Visual Basic .NET, go to the following URL: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vblr7/html/vastmfunction.asp
Next week, I'll talk about another type of overloading in the .NET Framework: operator overloading. Of all the elements in the .NET Framework, operator overloading is one of the few areas in which the .NET development team wasn't as consistent as they could've been.
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.
- 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. Save $2,990 when you register today, before your Early Bird discount expires.
(brought to you by Windows & .NET Magazine and its partners)
We need your opinion! Take our brief survey on managing Microsoft Exchange Server with third-party tools, and we'll automatically enter your name into a drawing for a Microsoft Xbox. Click here to start the survey!
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!
Raja has a Windows 2000 Server machine on which he installed Visual Studio .NET and all the latest patches. Now, he can't install any .msi files on this machine. If you can help, go to
4. NEW AND IMPROVED
(contributed by Sue Cooper, firstname.lastname@example.org)
DataDirect Technologies released DataDirect Connect for .NET, software that provides data-connectivity components to enable developers to access enterprise data in the Microsoft .NET environment. The connection between the ADO.NET interface in a .NET application and the data store is uniquely built with 100 percent managed code, running under the control of the Common Language Runtime (CLR). The resulting applications are faster and easier to create and deploy, offer better functionality and performance, and present less security risk. DataDirect Connect for .NET supports Oracle and Sybase on Windows XP, Windows 2000, Windows NT, Windows Me, and Windows 98. Pricing starts at $2500 per CPU. Contact DataDirect at 301-468-8501, 800-876-3101, or email@example.com.
5. CONTACT US
Here's how to reach us with your comments and questions:
- ABOUT DEVELOPER .NET PERSPECTIVES — firstname.lastname@example.org
- ABOUT THE NEWSLETTER IN GENERAL — email@example.com
(please mention the newsletter name in the subject line)
- TECHNICAL QUESTIONS — http://www.winnetmag.net/forums
- PRODUCT NEWS — firstname.lastname@example.org
- QUESTIONS ABOUT YOUR DEVELOPER .NET UPDATE SUBSCRIPTION?
Customer Support — Developer_dot_NET_update@winnetmag.com
- 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.