XLL+ Class Library

COper::ReadOpt() Examples

// Function:    OptArgs
// Returns:     LPXLOPER
// Description: Inspect optional arguments

//{{XLP_SRC(OptArgs)
    // NOTE - the FunctionWizard will add and remove mapping code here.
    //    DO NOT EDIT what you see in these blocks of generated code!
IMPLEMENT_XLLFN2(OptArgs, "RPPPP", "OptArgs", "OptNum,OptBool,Opt"
    "Long,OptString", "User Defined", "Inspect optional arguments"
    "", "Optional number\000Optional yes/no\000Optional integer"
    " value\000Optional string\000", "\0\0\0\0", 1)

extern "C" __declspec( dllexport )
LPXLOPER OptArgs(const COper* lpopOptNum, const COper*
    lpopOptBool, const COper* lpopOptLong, const COper*
    lpopOptString)
{
    CXlOper xloResult;
//}}XLP_SRC

    // Try to read the various arguments
    bool bValue;
    int iOptBool = lpopOptBool->ReadOpt(bValue, true);

    long lValue;
    int iOptLong = lpopOptLong->ReadOpt(lValue, 999L);
    
    CString strValue;
    int iOptString = lpopOptString->ReadOpt(strValue, "Default string");

    double dValue;
    int iOptNum = lpopOptNum->ReadOpt(dValue, 0.01);
    
    // Set up result array that reports on everything
    xloResult.AllocArray(4, 3);
    
    // Column 1 - the original input
    xloResult.Cell(0, 0) = *lpopOptBool;
    xloResult.Cell(1, 0) = *lpopOptLong;
    xloResult.Cell(2, 0) = *lpopOptString;
    xloResult.Cell(3, 0) = *lpopOptNum;
    
    // Column 2 - return value from ReadOpt()
    xloResult.Cell(0, 1) = (double)iOptBool;
    xloResult.Cell(1, 1) = (double)iOptLong;
    xloResult.Cell(2, 1) = (double)iOptString;
    xloResult.Cell(3, 1) = (double)iOptNum;
    
    // Column 3 - calculated value
    xloResult.Cell(0, 2) = (BOOL)bValue;
    xloResult.Cell(1, 2) = lValue;
    xloResult.Cell(2, 2) = strValue;
    xloResult.Cell(3, 2) = dValue;

    return xloResult.Ret();
}

// Function:    OptArgWithError
// Returns:     LPXLOPER
// Description: Demonstrates optional arguments with error handling

//{{XLP_SRC(OptArgWithError)
    // NOTE - the FunctionWizard will add and remove mapping code here.
    //    DO NOT EDIT what you see in these blocks of generated code!
IMPLEMENT_XLLFN2(OptArgWithError, "RPP", "OptArgWithError", 
    "OptLong,OptNum", "User Defined", "Demonstrates optional argu"
    "ments with error handling.", "Optional integer\000Optional n"
    "umber\000", "\0\0", 1)

extern "C" __declspec( dllexport )
LPXLOPER OptArgWithError(const COper* lpopOptLong, const COper*
    lpopOptNum)
{
    CXlOper xloResult;
//}}XLP_SRC

    BOOL bOk = TRUE;
    double dValue;
    long lValue;

    if ( bOk && !( bOk = (lpopOptLong->ReadOpt(lValue, 123) >= 0) ) )
        xloResult = "Error in OptLong: expected integer";
    if ( bOk && !( bOk = (lpopOptNum->ReadOpt(dValue, -1.0) >= 0) ) )
        xloResult = "Error in OptNum: expected number";
    
    if ( bOk )
        xloResult = dValue * (double)lValue;

    return xloResult.Ret();
}

Uses

COper::ReadOpt | CXlOper::Cell | CXlOper::AllocArray