You can add two values in an expression together with the + Operator (Visual Basic), or subtract one from another with the - Operator (Visual Basic), as the following example demonstrates.
Dim x As Integer
x = 67 + 34
x = 32 - 12
Negation also uses the - Operator (Visual Basic), but with only one operand, as the following example demonstrates.
Dim x As Integer = 65
Dim y As Integer
y = -x
Multiplication and division use the * Operator (Visual Basic) and / Operator (Visual Basic), respectively, as the following example demonstrates.
Dim y As Double
y = 45 * 55.23
y = 32 / 23
Exponentiation uses the ^ Operator (Visual Basic), as the following example demonstrates.
Dim z As Double
z = 23 ^ 3
' The preceding statement sets z to 12167 (the cube of 23).
Integer division is carried out using the \ Operator (Visual Basic). Integer division returns the quotient, that is, the integer that represents the number of times the divisor can divide into the dividend without consideration of any remainder. Both the divisor and the dividend must be integral types (SByte, Byte, Short, UShort, Integer, UInteger, Long, and ULong) for this operator. All other types must be converted to an integral type first. The following example demonstrates integer division.
Dim k As Integer
k = 23 \ 5
' The preceding statement sets k to 4.
Modulus arithmetic is performed using the Mod Operator (Visual Basic). This operator returns the remainder after dividing the divisor into the dividend an integral number of times. If both divisor and dividend are integral types, the returned value is integral. If divisor and dividend are floating-point types, the returned value is also floating-point. The following example demonstrates this behavior.
Dim x As Integer = 100
Dim y As Integer = 6
Dim z As Integer
z = x Mod y
' The preceding statement sets z to 4.
Dim a As Double = 100.3
Dim b As Double = 4.13
Dim c As Double
c = a Mod b
' The preceding statement sets c to 1.18.
Attempted Division by Zero
Division by zero has different results depending on the data types involved. In integral divisions (SByte, Byte, Short, UShort, Integer, UInteger, Long, ULong), the .NET Framework throws a DivideByZeroException exception. In division operations on the Decimal or Single data type, the .NET Framework also throws a DivideByZeroException exception.
In floating-point divisions involving the Double data type, no exception is thrown, and the result is the class member representing NaN, PositiveInfinity, or NegativeInfinity, depending on the dividend. The following table summarizes the various results of attempting to divide a Double value by zero.
Dividend data type
|
Divisor data type
|
Dividend value
|
Result
|
|---|
Double
|
Double
|
0
|
NaN (not a mathematically defined number)
|
Double
|
Double
|
> 0
|
PositiveInfinity
|
Double
|
Double
|
< 0
|
NegativeInfinity
|
When you catch a DivideByZeroException exception, you can use its members to help you handle it. For example, the Message property holds the message text for the exception. For more information, see Structured Exception Handling Overview for Visual Basic.