Defining Operators in VB .NET 2005
| Site editor's note: This article is based on a beta version of Visual Studio 2005. |
This article provides an introduction to defining operators in VB .NET 2005. This new language feature allows VB .NET users to specify operator methods on their own classes and structures. For example, an operator could be written to multiply together two Class1es.
Operators can be defined in Structures and Classes. They are generally called outside the class to which they belong, so they must be declared Public. They also don't belong to a specific instance of a type, but rather to the type, so they must be Shared. Operators must return a value, so there must be a return statement present for all code paths through the method. There are some additional rules for different kinds of operators.
Unary Operators
Unary operators are applied to one variable (for example, -x). The possible unary operators are +, -, isFalse, isTrue, and Not. A unary operator method can return any type but must take only one parameter, which must be the type of the containing class. For example:
Public Shared Operator -(ByVal Param1 As TwoNumbers) As TwoNumbers Return New TwoNumbers(-Param1.X, -Param1.Y) End Operator
Binary Operators
Binary operators are applied to two variables (as in x + y). The possible binary operators are +, -, *, /, \, ^, >>, <<, >, >=, <, <=, =, <>, And, Like, Mod, Or, and Xor. A binary operator method can return any type but must take two parameters, and at least one of them must be the type of the containing class. For example:
Public Shared Operator +(ByVal Param1 As TwoNumbers, _
ByVal Param2 As TwoNumbers) As TwoNumbers
Return New TwoNumbers(Param1.X + Param2.X, Param1.Y + Param2.Y)
End Operator
or
Public Shared Operator *(ByVal Param1 As TwoNumbers, _
ByVal Param2 As Integer) As Integer
Return (Param1.X * Param2) + (Param1.Y * Param2)
End Operator
Some operators must be added in pairs. For example, < cannot be defined for a type unless > is also defined. If one is present and not the other, an error message will appear.
Conversion Operators
Conversion operators are applied to a variable and a type, and return the value of the variable cast to the type (for example, CType(x, String)). The only conversion operator is CType. A conversion operator method returns the type to which the parameter will be cast. The parameter must be of the containing type, and there can only be one.
All conversion operators also must have either the Narrowing or Widening keyword. Adding the Widening keyword to a conversion operator specifies that it always succeeds at runtime and never incurs data loss. An example would be a conversion from a derived type to a base type. Adding the Narrowing keyword to a conversion operator specifies that it may not succeed at runtime, and could potentially fail or incur data loss. An example would be a conversion from a base type to a derived type.
For example:
Public Shared Narrowing Operator CType(ByVal Param1 As TwoNumbers) _ As Point Return New Point(Param1.X, Param1.Y) End Operator
Below is a more detailed example of a class with a number of Operators defined, and how those operators are called:
Imports System.Drawing
Class TwoNumbers
Private X As Integer
Private Y As Integer
Sub New(ByVal x2 As Integer, ByVal y2 As Integer)
X = x2
Y = y2
End Sub
' Called by 1
Public Shared Operator +(ByVal Param1 As TwoNumbers, _
ByVal Param2 As TwoNumbers) As TwoNumbers
Return New TwoNumbers(Param1.X + Param2.X, Param1.Y + Param2.Y)
End Operator
' Called by 2
Public Shared Operator -(ByVal Param1 As TwoNumbers) As TwoNumbers
Return New TwoNumbers(-Param1.X, -Param1.Y)
End Operator
' Called by 3
Public Shared Operator *(ByVal Param1 As TwoNumbers, _
ByVal Param2 As Integer) As Integer
Return (Param1.X * Param2) + (Param1.Y * Param2)
End Operator
' Called by 4
Public Shared Narrowing Operator CType(ByVal Param1 _
As TwoNumbers) As Point
Return New Point(Param1.X, Param1.Y)
End Operator
' Called by 5
Public Shared Operator >(ByVal Param1 As TwoNumbers, _
ByVal Param2 As TwoNumbers) As Boolean
Return (Param1.X + Param1.Y) > (Param2.X + Param2.Y)
End Operator
Public Shared Operator <(ByVal Param1 As TwoNumbers, _
ByVal Param2 As TwoNumbers) As Boolean
Return Not Param1 > Param2
End Operator
End Class
Module OverloadedOperatorModule
Sub Main()
Dim bool As Boolean
Dim int As Integer
Dim Var1 As New TwoNumbers(4, 5)
Dim Var2 As New TwoNumbers(2, -1)
Var1 = Var1 + Var2 'Calls 1
Var1 = -Var1 'Calls 2
int = Var2 * 4 'Calls 3
Dim pt As Point = CType(Var1, Point) 'Calls 4
bool = Var1 > Var2 'Calls 5
End Sub
End Module

Comments
There are no comments yet. Be the first to comment!