XLL+ Class Library (7.0)

Returning integer values

If you want to return an integer value to Excel using a CXlOper, you must first cast it to a double. If you do not, it will be treated as a boolean value.

Within Excel cells, all numeric values are stored as double (i.e. double-precision floating-point number), and the assignment operators for CXlOper reflect this.

Thus, the code below will return the value of result to Excel as a floating-point number.

CopyC++
CXlOper* INTFN_Impl(CXlOper& xloResult, ...)
{
    CXlOper xloResult;
//}}XLP_SRC 
 
    long result = CalculateAnInteger(...);
    xloResult = (double)result;
    return xloResult.Ret(); // Numeric value
}

Without the cast, the code below will return a boolean value, FALSE if result is zero, TRUE otherwise.

CopyC++
CXlOper* INTFN_Impl(CXlOper& xloResult, ...)
{
    CXlOper xloResult;
//}}XLP_SRC 
 
    long result = CalculateAnInteger(...);
    xloResult = result;
    return xloResult.Ret(); // Boolean value - TRUE if result != 0
}

Design note: Although it is possible for a CXlOper to hold an integer value, this type of CXlOper is only used for arguments passed to the Excel API, in order to call Excel built-in functions or macros. A more common use for CXlOper in add-in functions is to store boolean results. Therefore the integer version of the CXlOper assignment operator was originally reserved for boolean, rather than for integer values. This design decision followed from the absence of the keyword bool from early versions of Microsoft's C++ compiler. In order not to break existing code, this decision has not been changed since.

Next: Upgrading from a previous version of XLL+ >>