What Can You Overload?
You can overload pretty much any of Visual Basic's standard operators (except for
Is
and
IsNot
), plus a few other features. This section describes each
Public Shared Operator
XX
(...)
Mathematical Operators
Visual Basic defines ten mathematical or pseudo-mathematical operators. All but one of these exists to manipulate
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 also 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 Mathematical Operators
Comparison Operators
Visual Basic includes seven basic comparison operators, most often used in
If
statements and similar expressions that require a
Boolean
conditional calculation. The
Operator
All comparison operators are Boolean operators. Although you can alter the data types of the arguments passed to the operator, they must all return a Boolean value.
Public Shared Operator <=(ByVal operand1 As SomeClass, _
ByVal operand2 As SomeClass)
As Boolean
' ----- The <= operator returns a Boolean result.
End Operator
Table 12-2 lists six of the seven basic comparison operators that you can overload. Each entry includes a "buddy" value that identifies the matching operator that must also be overloaded. Table 12-2. The Overloadable Comparison Operators
The seventh comparison operator is
Like
. In standard Visual Basic, it compares the first operand to a string "pattern," which is
If (someValue Like somePattern) Then You don't have to use the same pattern rules when overloading the Like operator, and you can accept any data type for the pattern operand, but you must still return a Boolean result.
Public Shared Operator Like(ByVal operand1 As Bumblebee, _
ByVal operand2 As Integer)
As Boolean
' ----- See if Bumblebee matches an Integer pattern.
End Operator
There is no "buddy" operator that you must implement when overloading the Like operator. Bitwise and Logical OperatorsAmong the logical and bitwise operators included in Visual Basic, four already perform double duty as overloaded operators. The bitwise And , Or , Xor , and Not accept integer operands, generating a numeric result with values transformed at the individual bit level. They also work as logical operators, accepting and returning Boolean values, most often in conditional statements. But they can handle the stress of being overridden a little more. When you do override these four operators, you are overriding the bitwise versions, not the logical versions. Basically, this means that you have control over the return value, and aren't required to make it Boolean . Table 12-3 lists the eight overloadable bitwise and logical operators. Table 12-3. The Overloadable Bitwise and Logical Operators
The CType OperatorThe Visual Basic CType feature looks more like a function than an operator:
result = CType( source , type )
But looks are deceiving. It is not a true function, and as with the other conversion functions (like
CInt
), it is actually
Public Shared Operator CType(ByVal operand1 As Bumblebee) _
As Integer
' ----- Perform conversion here, returning an Integer.
End Operator
If you try to type that last block of code into Visual Basic, it will complain that you are missing either the "Widening" or "Narrowing" keyword (see Figure 12-1). Figure 12-1. Visual Basic complains about all things wide and narrow
I mentioned widening and narrowing conversions in passing in Chapter 2, "Introducing Visual Basic," but let's examine them in more depth. When you convert between some
Dim quiteBig As Short = 5000 Dim quiteSmall As Byte ' ----- These next two lines will fail. quiteSmall = quiteBig quiteSmall = CByte(quiteBig) And it's obvious why it fails: A Byte variable cannot hold the value 5000. But what about this code?
Dim quiteBig As Short = 5 Dim quiteSmall As Byte ' ----- These next two lines will succeed. quiteSmall = quiteBig quiteSmall = CByte(quiteBig)
It will run just fine, because 5 fits into a
Byte
variable with room to spare. (If
Option Strict
is set to
On
, the first assignment will still fail to compile.) Still, there is nothing to stop me from
When a conversion has the potential to fail due to the source data not being able to fully fit in the target variable, it's called a narrowing conversion . Narrowing conversions are a reality, and as long as you have checked the data before the conversion, there shouldn't be any reason to permanently restrict such conversions.
Widening conversions
go in the
Visual Basic allows widening conversions to occur automatically, implicitly. You don't have to explicitly use CType to force the conversion. If you had a widening conversion from Bumblebee to Integer , and you had set Option Strict to On , the following code would work just fine.
Dim sourceValue As New Bumblebee Dim destValue As Integer = sourceValue If the conversion from Bumblebee to Integer was narrowing, you would have to force the conversion using CType just so Visual Basic was sure you really wanted to do this.
Dim sourceValue As New Bumblebee Dim destValue As Integer = CType(sourceValue, Integer)
When you create custom conversions with the overloaded
CType
operator, you must
Public Shared
Narrowing
Operator CType( _
ByVal operand1 As Bumblebee) As Integer
' ----- Perform narrowing conversion here.
End Operator
|

Expert One-on-One Visual Basic 2005 Design and Development

Visual Basic 2005 Cookbook: Solutions for VB 2005 Programmers (Cookbooks (O'Reilly))

Microsoft Visual Basic 2005 Step by Step (Step by Step (Microsoft))

Beginning VB 2005 Databases: From Novice to Professional (Beginning: From Novice to Professional)