One of the new features we're adding to Visual Basic is called Operator Overloading.  This feature allows the programmer to create a class that knows what +, -, *, and other operators mean.

 

There are two main components to operator overloading: consumption and declaration. (Actually, I've just described a major precept of language design. Although they go hand-in-hand, the declaration form of a feature and the consumption form of a feature are two intrinsically different concepts. The dichotomy reveals itself even in .NET executables. When people talk about "metadata", they are referring to declarations. When people talk about intermediate language (IL) opcodes, they are referring to consumption.)

 

Let's take a look at consumption first with an example.The System.Drawing namespace contains the types Point and Size. A new point can be calculated by adding or subtracting a size from an already existing point. Programmatically, the + and – operators lend themselves naturally to this calculation:

 

Dim p1 As New Point(10, 10)

Dim s1 As New Size(20, -10)

 

Dim p3 As Point = p1 + s1

Dim p4 As Point = p1 - s1

 

MsgBox(p3.ToString)

MsgBox(p4.ToString)

 

Because Point overloads the + and - operators, this code compiles and works as expected, outputting:

 

{X=30,Y=0}

{X=-10,Y=20}

 

How was this done? When the compiler analyzes the expression "p1 + s1", it discovers the type of "p1" is Point and that Point defines Operator +. Operators are actually just functions with funny names, so the compiler turns "p1 + s1" into a function call, equivalently written as:

 

Dim p3 As Point = System.Drawing.Point.op_Addition(p1, s1)

 

This is the key to understanding overloaded operators: they are just functions. An expression like "a + b" is really a call to the + operator with two arguments: "+(a, b)". Because member names cannot contain symbols like "+", the compiler uses a special name behind the scenes: "op_Addition(a, b)". And as functions, the compiler uses normal overload resolution rules to pick the correct overload (in exactly the same way the compiler uses these rules to pick the correct overload of Console.WriteLine, which at last count had 19 overloads!).

 

Perhaps you can guess what the declarations of overloaded operators look like; they're an awful lot like functions:

 

Public Shared Operator +(ByVal Left As Point, ByVal Right As Size) As Point

    Return New Point(Left.X + Right.Width, Left.Y + Right.Height)

End Operator

 

Public Shared Operator -(ByVal Left As Point, ByVal Right As Size) As Point

    Return New Point(Left.XRight.Width, Left.YRight.Height)

End Operator

 

The Visual Basic compiler will allow you to overload several operators, summarized in the table below:

 

+

+ (unary)

-

- (unary)

*

\

/

^

&

Like

Mod

And

Or

Xor

Not

<< 

>> 

= (comparison only)

<> 

< 

<=

> 

>=

CType

IsTrue

IsFalse

 

 

CType, IsTrue and IsFalse are special and I'll cover those in a future entry. But quickly, you can overload conversions between types using the CType operator (instead of overloading the = assignment operator). And IsTrue and IsFalse are for overloading AndAlso and OrElse which are themselves not directly overloadable.

 

A few rules greatly simplify the design and use of operator overloading: each operator declaration must be Public and Shared, and must be declared in the same type as one of its operands. And some operators, like <= and >=, must be pair-wise declared, i.e., a declaration of one requires an identical declaration of the other. You'll find, as I did, that these simple rules make understanding operator overloading much, much easier.

 

More info to follow...