GOTCHA 33 Name collision with keywords breaks interoperability


GOTCHA #33 Name collision with keywords breaks interoperability

When choosing names for methods or properties, avoid names that are keywords in other .NET languages. Most of the time this won't be an issue, for you would be unlikely to choose a name like new, structure, or class. However, I've been surprised at times by the names I chose. Consider Example 4-7.

Example 4-7. Method name conflict

C# and VB.NET (WhatsInAName)

   //StopWatch.cs part of CSLib.dll using System;   namespace CSLib {     public class StopWatch     {         public virtual void Start() {}           public virtual void Stop() {}           public virtual void Reset()         {         }           public int ElapasedTime         {             get { return 0; }         }     } } 

A C# class named StopWatch models a stopwatch that will allow you to measure the time spent in certain activities or events. It has a method named Start() that gets the stopwatch going. The Stop() method halts it. Reset() is used to simply clear the stopwatch. The ElapsedTime property gives you the time that has elapsed. Let's just leave the methods and property with a dummy implementation for this example. The code compiles without any error. So, what's the problem?

Let's create a VB.NET class that derives from this, as in Example 4-8.

Example 4-8. A VB.NET class derived from the class in Example 4-7

C# and VB.NET (WhatsInAName)

   'SpecialStopWatch.vb part of VBLib.dll   Public Class SpecialStopWatch     Inherits CSLib.StopWatch       Public Overrides Sub Reset()       End Sub       Public Overrides Sub Start()       End Sub       Public Overrides Sub Stop()       End Sub End Class 

When you compile the code you get an error, as in Figure 4-5.

Figure 4-5. Error compiling the class in Example 4-8


The problem here is that Stop is a keyword in VB.NET. A Stop statement suspends execution of the code. Yes, you do want to suspend the execution of the StopWatch, but not the execution of the program. But there is a workaround in this case. You can write the method as shown in Figure 4-6.

Figure 4-6. Workaround to override Stop() method


This allows the code to compile. When invoking the method on an object of SpecialStopWatch, you simply call the Stop() method like you call any other method: obj.Stop(), where obj stands for the name of the method.

While VB.NET escapes the conflicting methods by placing the name within [], C# uses an @ prefix. For instance, say you have a method named override() in a VB.NET class. If you derive from that class in C# and override the method named override(), you have to write it as shown in Figure 4-7.

Figure 4-7. Keyword prefix in C#


When calling this method, you write obj.@override(). This works, but it's ugly.

IN A NUTSHELL

When choosing names (for methods, properties, etc), use caution not to use names that are keywords in other languages. The other languages may have to use escape characters to access these conflicting names. Of course, as more languages become available in the .NET environment, you can't possibly know all their keywords. But if you know this is an issue, at least it won't surprise you if you bump into it.

SEE ALSO

Gotcha #31, "Optional parameters break interoperability," Gotcha #32, "Mixing case between class members breaks interoperability," and Gotcha #34, "Defining an array isn't consistent."



    .NET Gotachas
    .NET Gotachas
    ISBN: N/A
    EAN: N/A
    Year: 2005
    Pages: 126

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net