Data Types of Operator Results (Visual Basic)

 

Updated: July 20, 2015

For the latest documentation on Visual Studio 2017 RC, see Visual Studio 2017 RC Documentation.

Visual Basic determines the result data type of an operation based on the data types of the operands. In some cases this might be a data type with a greater range than that of either operand.

The ranges of the relevant data types, in order from smallest to largest, are as follows:

  • Boolean — two possible values

  • SByte, Byte — 256 possible integral values

  • Short, UShort — 65,536 (6.5...E+4) possible integral values

  • Integer, UInteger — 4,294,967,296 (4.2...E+9) possible integral values

  • Long, ULong — 18,446,744,073,709,551,615 (1.8...E+19) possible integral values

  • Decimal — 1.5...E+29 possible integral values, maximum range 7.9...E+28 (absolute value)

  • Single — maximum range 3.4...E+38 (absolute value)

  • Double — maximum range 1.7...E+308 (absolute value)

For more information on Visual Basic data types, see Data Types.

If an operand evaluates to Nothing, the Visual Basic arithmetic operators treat it as zero.

Note that the Decimal data type is neither floating-point nor integer.

If either operand of a +, , *, /, or Mod operation is Decimal and the other is not Single or Double, Visual Basic widens the other operand to Decimal. It performs the operation in Decimal, and the result data type is Decimal.

Visual Basic performs most floating-point arithmetic in Double, which is the most efficient data type for such operations. However, if one operand is Single and the other is not Double, Visual Basic performs the operation in Single. It widens each operand as necessary to the appropriate data type before the operation, and the result has that data type.

/ and ^ Operators

The / operator is defined only for the Decimal, Single, and Double data types. Visual Basic widens each operand as necessary to the appropriate data type before the operation, and the result has that data type.

The following table shows the result data types for the / operator. Note that this table is symmetric; for a given combination of operand data types, the result data type is the same regardless of the order of the operands.

DecimalSingleDoubleAny integer type
DecimalDecimalSingleDoubleDecimal
SingleSingleSingleDoubleSingle
DoubleDoubleDoubleDoubleDouble
Any integer typeDecimalSingleDoubleDouble

The ^ operator is defined only for the Double data type. Visual Basic widens each operand as necessary to Double before the operation, and the result data type is always Double.

The result data type of an integer operation depends on the data types of the operands. In general, Visual Basic uses the following policies for determining the result data type:

  • If both operands of a binary operator have the same data type, the result has that data type. An exception is Boolean, which is forced to Short.

  • If an unsigned operand participates with a signed operand, the result has a signed type with at least as large a range as either operand.

  • Otherwise, the result usually has the larger of the two operand data types.

Note that the result data type might not be the same as either operand data type.

System_CAPS_ICON_note.jpg Note

The result data type is not always large enough to hold all possible values resulting from the operation. An OverflowException exception can occur if the value is too large for the result data type.

Unary + and – Operators

The following table shows the result data types for the two unary operators, + and .

BooleanSByteByteShortUShortIntegerUIntegerLongULong
Unary +ShortSByteByteShortUShortIntegerUIntegerLongULong
Unary ShortSByteShortShortIntegerIntegerLongLongDecimal

<< and >> Operators

The following table shows the result data types for the two bit-shift operators, << and >>. Visual Basic treats each bit-shift operator as a unary operator on its left operand (the bit pattern to be shifted).

BooleanSByteByteShortUShortIntegerUIntegerLongULong
<<, >>ShortSByteByteShortUShortIntegerUIntegerLongULong

If the left operand is Decimal, Single, Double, or String, Visual Basic attempts to convert it to Long before the operation, and the result data type is Long. The right operand (the number of bit positions to shift) must be Integer or a type that widens to Integer.

Binary +, –, *, and Mod Operators

The following table shows the result data types for the binary + and operators and the * and Mod operators. Note that this table is symmetric; for a given combination of operand data types, the result data type is the same regardless of the order of the operands.

BooleanSByteByteShortUShortIntegerUIntegerLongULong
BooleanShortSByteShortShortIntegerIntegerLongLongDecimal
SByteSByteSByteShortShortIntegerIntegerLongLongDecimal
ByteShortShortByteShortUShortIntegerUIntegerLongULong
ShortShortShortShortShortIntegerIntegerLongLongDecimal
UShortIntegerIntegerUShortIntegerUShortIntegerUIntegerLongULong
IntegerIntegerIntegerIntegerIntegerIntegerIntegerLongLongDecimal
UIntegerLongLongUIntegerLongUIntegerLongUIntegerLongULong
LongLongLongLongLongLongLongLongLongDecimal
ULongDecimalDecimalULongDecimalULongDecimalULongDecimalULong

\ Operator

The following table shows the result data types for the \ operator. Note that this table is symmetric; for a given combination of operand data types, the result data type is the same regardless of the order of the operands.

BooleanSByteByteShortUShortIntegerUIntegerLongULong
BooleanShortSByteShortShortIntegerIntegerLongLongLong
SByteSByteSByteShortShortIntegerIntegerLongLongLong
ByteShortShortByteShortUShortIntegerUIntegerLongULong
ShortShortShortShortShortIntegerIntegerLongLongLong
UShortIntegerIntegerUShortIntegerUShortIntegerUIntegerLongULong
IntegerIntegerIntegerIntegerIntegerIntegerIntegerLongLongLong
UIntegerLongLongUIntegerLongUIntegerLongUIntegerLongULong
LongLongLongLongLongLongLongLongLongLong
ULongLongLongULongLongULongLongULongLongULong

If either operand of the \ operator is Decimal, Single, or Double, Visual Basic attempts to convert it to Long before the operation, and the result data type is Long.

The result data type of a relational operation (=, <>, <, >, <=, >=) is always BooleanBoolean Data Type. The same is true for logical operations (And, AndAlso, Not, Or, OrElse, Xor) on Boolean operands.

The result data type of a bitwise logical operation depends on the data types of the operands. Note that AndAlso and OrElse are defined only for Boolean, and Visual Basic converts each operand as necessary to Boolean before performing the operation.

=, <>, <, >, <=, and >= Operators

If both operands are Boolean, Visual Basic considers True to be less than False. If a numeric type is compared with a String, Visual Basic attempts to convert the String to Double before the operation. A Char or Date operand can be compared only with another operand of the same data type. The result data type is always Boolean.

Bitwise Not Operator

The following table shows the result data types for the bitwise Not operator.

BooleanSByteByteShortUShortIntegerUIntegerLongULong
NotBooleanSByteByteShortUShortIntegerUIntegerLongULong

If the operand is Decimal, Single, Double, or String, Visual Basic attempts to convert it to Long before the operation, and the result data type is Long.

Bitwise And, Or, and Xor Operators

The following table shows the result data types for the bitwise And, Or, and Xor operators. Note that this table is symmetric; for a given combination of operand data types, the result data type is the same regardless of the order of the operands.

BooleanSByteByteShortUShortIntegerUIntegerLongULong
BooleanBooleanSByteShortShortIntegerIntegerLongLongLong
SByteSByteSByteShortShortIntegerIntegerLongLongLong
ByteShortShortByteShortUShortIntegerUIntegerLongULong
ShortShortShortShortShortIntegerIntegerLongLongLong
UShortIntegerIntegerUShortIntegerUShortIntegerUIntegerLongULong
IntegerIntegerIntegerIntegerIntegerIntegerIntegerLongLongLong
UIntegerLongLongUIntegerLongUIntegerLongUIntegerLongULong
LongLongLongLongLongLongLongLongLongLong
ULongLongLongULongLongULongLongULongLongULong

If an operand is Decimal, Single, Double, or String, Visual Basic attempts to convert it to Long before the operation, and the result data type is the same as if that operand had already been Long.

The & operator is defined only for concatenation of String operands. Visual Basic converts each operand as necessary to String before the operation, and the result data type is always String. For the purposes of the & operator, all conversions to String are considered to be widening, even if Option Strict is On.

The Is and IsNot operators require both operands to be of a reference type. The TypeOf...Is expression requires the first operand to be of a reference type and the second operand to be the name of a data type. In all these cases the result data type is Boolean.

The Like operator is defined only for pattern matching of String operands. Visual Basic attempts to convert each operand as necessary to String before the operation. The result data type is always Boolean.

Data Types
Operators and Expressions
Arithmetic Operators in Visual Basic
Comparison Operators in Visual Basic
Operators
Operator Precedence in Visual Basic
Operators Listed by Functionality
Arithmetic Operators
Comparison Operators
Option Strict Statement

Show: