Operator overloads are marked with both Shared and Public and must have at least one
parameter in their declaration that matches the enclosing type. Since these methods will be
inherited by derived classes, it is impossible for the derived type??™s operator method to match
the signature of the base class operator method exactly. For example, the GreenApple class in
the following sample is not valid:
Public Class Apple
Public Shared Operator +(ByVal rhs As Apple, ByVal lhs As Apple) As Apple
'Method does nothing & exists only for example.
Return rhs
End Operator
End Class
Public Class GreenApple
Inherits Apple
'INVALID - This will not compile!
Public Overloads Shared Operator +(ByVal rhs As Apple, ByVal lhs As Apple) _
As Apple
'Method does nothing & exists only for example.
Return rhs
End Operator
End Class
If you attempt to build the previous code, you??™ll get the following compiler error:
At least one parameter of this binary operator
must be of the containing type 'GreenApple'
Operators Shouldn??™t Mutate Their Operands
You already know that operator methods are Shared. Therefore, it is highly recommended
that you do not mutate (or directly modify) the operands passed into the operator methods.
Instead, you should create a new instance of the return value type and return the result of the
operation.
Pages:
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211