Function Coerce Make Class

CoerceToLong

"Coerce String To Long" Return the Long (integer) value whose in-memory representation is the same as the first four characters in string vString. Same as the CVL function in earlier versions of Microsoft Basic. Inverse of the MakeLong (MKL) function.

Signature (Visual Basic / VBA)

Public Function CoerceToLong( _
    ByVal vString As Variant _
    ) As Variant

Code Example

    CoerceToLong(String$(4, 0)) = 0&
    CoerceToLong(Chr$(1) + Chr$(0) + Chr$(0) + Chr$(0)) = 1&
    CoerceToLong(Chr$(0) + Chr$(1) + Chr$(0) + Chr$(0)) = 256&
    CoerceToLong(String$(4, 255)) = -1&
    CoerceToLong(String$(4, 254)) = -16843010&
    IsNull(CoerceToLong(Null)) = True

Parameters

Parameter Description
vString The string whose first four characters are to be interpreted as a Long value. Function returns Null if vString is Null or cannot be fixed up to a String.

Notes & Version History

Note Function generates the Visual Basic Error "Illegal Function Call" if vString is less than 4 characters long (the size of a Long value).
Note If vString is more than four characters long, the leftmost four characters are coerced to a Long value.
Note This function first converts the string from Unicode to the default code page of the system.
v1.3 Change Removed 16-bit support from this function.