Variables of one type can be converted into another type. This is often called Casting.
Casting can either be narrowing or widening
There are two mechanisms that can be used to accomplish this conversion: implicit or explicit conversion
This is one in which the conversion is to a data type that can accommodate every possible value in the existing data type.
For example a Long can accommodate every possible value held by an Integer.
Casting from an Integer to a Long is a widening conversion.
This is one in which the conversion is to a data type that does not accommodate every possible value in the existing data type.
For example an Integer cannot be accommodate every possible value held by an Long.
Casting from a Long to an Integer is a narrowing conversion.
Is invoked automatically when a value of one data type is assigned to a value of another.
Widening casts are often implicit.
Is done using one of the data type conversion functions listed below.
This is more risky as there is a possibility that some data or significant figures can be lost.
Consider converting a single value to an Integer.
Since a single value takes up to 4 bytes and an Integer takes up to 2 bytes, some data will be lost.
Narrowing casts must be explicit.
In VBA converts all integer values to long, even if they have been declared as type Integer.
It may even be faster just to always use Long so VBA doesn't have to convert it.
© 2021 Better Solutions Limited. All Rights Reserved. © 2021 Better Solutions Limited TopPrevNext