XLL+ Class Library (6.3)

MyDaysInFeb Example

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

CopyC++
// 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_XLLFN3(MyDaysInFeb, MyDaysInFeb_4, MyDaysInFeb_12, "RP", "UQ", L"MyD"
    L"aysInFeb", 0, L"Year", 0, L"14", 0, L"Return the number of days in Febru"
    L"ary in a year", 0, L"Optional year as a number (Default = 1997)\0", 0, 0, 
    L"{MyDaysInFeb,,,Return the number of days in February in a year,14,1,0,U,"
    L"{{0,{Year,Value,0,,Optional year as a number (Default = 1997),,,,}}},{},"
    L"3,,0,0}", 1)
CXlOper* MyDaysInFeb_Impl(CXlOper&, const CXlOper*);

extern "C" __declspec(dllexport)
LPXLOPER12 MyDaysInFeb_12(LPXLOPER12 Year)
{
    XLL_FIX_STATE;
    CXlOper xloResult, Year__port(Year);
    try {
        CXlStructuredExceptionHandler _seh_;
        xloResult.HandleResult(MyDaysInFeb_Impl(xloResult, &Year__port));
    }
    catch(const CXlRuntimeException& ex) {
        CXllApp::Instance()->DisplayException(xloResult, ex);
    }
    return xloResult.Ret12();
}
extern "C" __declspec(dllexport)
LPXLOPER4 MyDaysInFeb_4(LPXLOPER4 Year)
{
    XLL_FIX_STATE;
    CXlOper xloResult, Year__port(Year);
    try {
        CXlStructuredExceptionHandler _seh_;
        xloResult.HandleResult(MyDaysInFeb_Impl(xloResult, &Year__port));
    }
    catch(const CXlRuntimeException& ex) {
        CXllApp::Instance()->DisplayException(xloResult, ex);
    }
    return xloResult.Ret4();
}

CXlOper* MyDaysInFeb_Impl(CXlOper& xloResult, const CXlOper* Year)
{
    // End of generated code 
//}}XLP_SRC 
 
    unsigned short usYear = 0;

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

    // If argument is a double, use it 
    else if ( Year->IsDouble() )
    {
        double dYear = Year->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

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