You can overload pretty much any of Visual Basic's standard operators (except for
IsNot), plus a few other features. This section describes each overloadable operator, grouped by general type. Each section includes a table of operators. To overload an operator in a class, use the name in the Operator column as the function name. If there were an operator named
XX, the matching
Operator statement would be as follows:
Public Shared Operator XX(...)
Visual Basic defines 10 mathematical or pseudo-mathematical operators. All but one of these exists to manipulate numbers. The leftover operator is the string concatenation operator (
&), which works with strings, but kind of looks like the other mathematical operators in its syntax and use.
Two of the operators, plus (
+) and minus (
−), are both unary and binary operators. The minus sign (
−) works as a unary "negation" operator (as in "−5") and as a binary "subtraction" operator (the common "5 - 2" syntax). When overloading these operators, the difference lies in the number of arguments included in the argument signature.
Public Shared Operator -(ByVal operand1 As SomeClass, _ ByVal operand2 As SomeClass) As SomeClass ' ----- This is the binary "subtraction" version. End Operator Public Shared Operator -(ByVal operand1 As SomeClass) _ As SomeClass ' ----- This is the unary "negation" version. End Operator
Table 12-1 lists the mathematical operators that support overloading.
Table 12-1. The overloadable ...