Developer .NET UPDATE, September 10, 2002

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


THIS ISSUE SPONSORED BY

Connections .NET Tour
http://www.devconnections.com/tour

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


SPONSOR: 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. Join industry gurus Paul Litwin, Ken Getz, and Richard Hale Shaw for these power-packed seminars.

Monterey, CA
September 23 & 24

Washington DC
September 30 & October 1

Redmond, WA (Special Day 1 only on Microsoft's Campus)
October 4

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. As an extra bonus, you'll get a FREE one night stay at the host hotel when you register for both days.

Don't let a lean travel budget prevent you from keeping your competitive edge as a developer. Reserve your seat today at:
http://www.devconnections.com/tour/fall2002/registrationoffers.asp?refer=


September 10, 2002—In this issue:

1. DEVELOPER .NET PERSPECTIVES

  • Operator Overloading

2. ANNOUNCEMENTS

  • The Security Solutions You've Been Searching For!
  • UNIX, Linux, and Windows: Managing the Unruly Trinity

3. RESOURCE

  • Featured Thread: Buggy Software Is an Industry-Wide Problem

4. NEW AND IMPROVED

  • Protect Your Intellectual Property

5. CONTACT US

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

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

  • OPERATOR OVERLOADING

  • By and large, Visual Basic .NET and Visual C# .NET have comparable features. However, one of the most notable differences is the inclusion of operator overloading in Visual C# .NET but not in Visual Basic .NET.

    With operator overloading, you can create a custom definition for an operator so that when you combine that operator with an instance of a specific class, the operator takes on a special meaning. The simplest way to understand how operator overloading works is to consider the automatic, or implicit, conversion that occurs when you use the addition (+) operator to add an Integer value and a Long value. The underlying system code recognizes that an Integer value is 4 bytes and a Long value is 8 bytes. The underlying system code automatically moves the value associated with the Integer to a location appropriate to the Long value and carries out the addition operation, the result of which is a Long value. In a nutshell, the underlying system code lets you use shorthand to convert one data type to another.

    In Visual C# .NET, implicit conversions are limited to type-safe conversions, such as the one I just described, unless you use a special keyword (more on this keyword shortly). Similarly, Visual Basic .NET limits implicit conversions to type-safe conversions when you set the Option Strict option to On. (To access this option, open a project's Property Pages, click the Common Properties folder, and select the Build property page.) When you enable Option Strict, you can't perform late bindings. More important, you can't perform "implicit narrowing conversions." In other words, you can't perform data-type conversions that result in lost data or conversions between numeric data types and strings. If you leave Option Strict set to Off (the default), you can include implicit narrowing conversions in your code without getting a compiler error. In general, however, enabling Option Strict is best for production-quality code.

    Although Visual C# .NET doesn't have a feature comparable to Option Strict, you can use the Operator keyword to overload an operator to get behavior similar to the behavior that occurs when Option Strict is off. Operator overloading lets a class provide custom operations far beyond those available as implicit conversions. For example, you can define a method that overloads the + operator in MyClass1 so that MyClass1 accepts instances of MyClass1 and MyClass2 data. When you create an overloaded + method, you need to create a second custom method called Add that performs the same operation. The following declarations declare both the custom overloaded + method and its companion Add method:

    public static MyClass1 operator + (MyClass1 X, MyClass2 Y)
    public static MyClass1 Add (MyClass1 X, MyClass2 Y)

    After you define the custom operator's behavior, you can use the overloaded + operator to "add" the data from a MyClass1 object to a MyClass2 object. Thus, even if these objects store data differently, you can still combine their contents.

    You can use overloaded operators in many different types of operations. For example, you might use an overloaded equality (

    ) operator to define a custom test for equality in an If...Then...Else statement. Note that if you overload the

    operator, you must also overload its counterpart, the not equal (!=) operator.

    One major limitation of Visual C# .NET is that it doesn't support overloading the assign (=) operator. Although support for overloaded = operators is one of the more powerful features of object-oriented (OO) languages such as C++, this capability isn't available in Visual C# .NET.

    As I mentioned previously, Visual Basic .NET doesn't support overloading operators of any kind. Although this omission won't break any code outright, it creates problems for developers who use multiple Microsoft .NET languages. In my opinion, not providing this feature in both languages is a mistake on the part of the .NET Framework developers.

    Fortunately, workarounds exist. For example, if you want to use a Visual Basic .NET class in a Visual C# .NET project and you want to include an overloaded + operator in the class, you must first create a new class that inherits the Visual Basic .NET class, then create a custom override of the operator. Essentially, you must encapsulate the Visual Basic .NET implementation's call to the Add method in a custom Visual C# .NET wrapper class.

    Conversely, if you're working on a Visual Basic .NET project and you want to use a Visual C# .NET class that includes an overloaded + operator but not the corresponding Add method, you're limited to creating an alternative Add method in Visual C# .NET. (Ideally, the class's original developer should provide the Add method.) You can create an alternative Add method in two ways. The first option is to implement a Visual Basic .NET version of the Add method. To do this, you need to know the underlying implementation of the overloaded + operator in the Visual C# .NET class.

    If you want to avoid rewriting the implementation, another option is to create a Visual C# .NET class that inherits the library class, then implement an Add method that uses the overloaded + operator. Your Visual Basic .NET code can then call this customized version of the library class. With this approach, the Visual C# .NET implementation can be as simple as

    public static MyClass1 Add (MyClass1 X, MyClass2 Y)
    \{
       return X + Y;
    \}

    The following Visual C# .NET code shows not only operator overloading but also method overloading, which I discussed last week:

    public class Employee : CSharpLibrarySample.Person
    \{
       // Overloading. Note that as long as parameter list
          // varies the return type can vary.
          public decimal CalculatePay(float Salary) 
       \{
          return Convert.ToDecimal(Salary / 52);
       \}
       public float CalculatePay(float rate, float hours) 
       \{
          return rate * hours;
       \}
          // Operator overloading for the comparison 

    operator. // This operator must be matched with a != overload. public static bool operator

    (Employee X, Person Y) \{ return X.PersonID

    Y.PersonID; \} public static bool operator

    (Employee X, Employee Y) \{ return X.PersonID

    Y.PersonID; \} public static bool equals (Employee X, Person Y) \{ return X.PersonID

    Y.PersonID; \} ... public static bool operator != (Employee X, Person Y) \{ return X.PersonID != Y.PersonID; \} ... \}

    For more information about how to overload operators, go to the following URLs:

    http://msdn.microsoft.com/library/default.asp?url=/library/ en-us/cpgenref/html/cpconoperatoroverloadingusageguidelines.asp

    http://msdn.microsoft.com/library/default.asp?url=/library/ en-us/csref/html/vclrfoverloadableoperators.asp

    http://msdn.microsoft.com/library/default.asp?url=/library/ en-us/csref/html/vclrfoperator.asp

    Operator overloading is a powerful tool available in most traditional OO languages. Its absence in the Visual Basic .NET toolbox is unfortunate if you plan on using several .NET languages to develop applications. The need to constantly create alternative language-neutral implementations limits operator overloading's value in the current .NET Framework. However, the omission of operator overloading in Visual Basic .NET and other .NET languages doesn't limit your ability to develop applications. Not only can you take advantage of features such as Visual Basic .NET's Option Strict, but you can also use named methods to perform the same type of operations that you can perform with overloaded operators.


    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.
    https://secure.win2000mag.com/events/sql_register.asp


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

  • THE SECURITY SOLUTIONS YOU'VE BEEN SEARCHING FOR!

  • Our popular Interactive Product Guides (IPGs) are online catalogs of the hottest vendor solutions around. Our latest IPG highlights the security solutions and services that will help you protect your data and your network before disaster strikes. Check it out at:
    http://www.winntmag.com/techware/interactiveproduct/security

  • UNIX, LINUX, AND WINDOWS: MANAGING THE UNRULY TRINITY

  • Sign up for our latest Web seminar at which we'll discuss the concerns associated with managing a heterogeneous server environment. You'll learn more about the management characteristics of each platform and about existing management solutions and how well they work. Sponsored by NetIQ. There's no charge for this online event, but space is limited so register now at
    http://www.winnetmag.com/seminars/netiq

    3. RESOURCE

  • FEATURED THREAD: BUGGY SOFTWARE IS AN INDUSTRY-WIDE PROBLEM

  • Brian Valentine, senior vice president of Microsoft's Windows development, made a grim admission at the Windows .NET Server DevCon 2002 in Seattle on September 6. To find out what he said, go to
    http://63.88.172.222/forums/messageview.cfm?catid=7&threadid=34927

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

  • PROTECT YOUR INTELLECTUAL PROPERTY

  • WiseOwl released Demeanor for .NET, Enterprise Edition, a code obfuscator for the Microsoft .NET platform. Demeanor for .NET makes your applications difficult to reverse engineer by changing the names of your types, fields, methods, properties, and events to meaningless symbols. It also obfuscates your applications' metadata and alters your methods' control flow to make the resulting code difficult to understand. Demeanor for .NET builds a new Portable Executable (PE) file for each assembly. The product uses an efficient memory layout that results in a PE file size that's an average of 10 percent smaller than the original, so your application loads and downloads more quickly. Demeanor for .NET supports any language within the .NET Framework. Priced at $1250. Contact WiseOwl at 760-471-9833 and [email protected]
    http://www.wiseowl.com

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

    (please mention the newsletter name in the subject line)

    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

    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