XLL+ Class Library

MyDaysInFeb Example

This example demonstrates how to check for the presence or absence of optional arguments.

// Function:    MyDaysInFeb
// Returns:     LPXLOPER
// Description: Return the number of days in February in a year

//{{XLP_SRC(MyDaysInFeb)
    // NOTE - the FunctionWizard will add and remove mapping code here.
    //    DO NOT EDIT what you see in these blocks of generated code!
IMPLEMENT_XLLFN2(MyDaysInFeb, "RP", "MyDaysInFeb", "Year", 
    "User Defined", "Return the number of days in February in a"
    " year", "Optional year as a number (Default = 1997)\000", 
    "\0", 1)

extern "C" __declspec( dllexport )
LPXLOPER MyDaysInFeb(const COper* lpopYear)
{
    CXlOper xloResult;
//}}XLP_SRC

    unsigned short usYear = 0;

    // If argument is missing, use default year value
    if ( lpopYear->IsMissing() )
        usYear = 1997;

    // If argument is a double, use it
    else if ( lpopYear->IsDouble() )
    {
        double dYear = lpopYear->ToDouble();
        if ( dYear < 1904.0 || dYear > 2099.0 )
            xloResult = xlerrValue;
        else
            usYear = (unsigned short)dYear;
    }

    // Any other data type is unacceptable
    else 
        xloResult = xlerrValue;

    // Get result if no error
    if ( usYear > 0 )
        xloResult = (double)((usYear % 4 != 0) ? 28 : 29);

    return xloResult.Ret();
}

Uses

COper::IsMissing | COper::IsDouble | CXlOper::operator =