Default argument without declaration in AddInX routine

Aug 22, 2013 at 9:59 AM
Edited Aug 22, 2013 at 9:59 AM
I've develop function that take 2 doubles and return 1 double and have 3rd default argument of bool type:
static AddInX test(
    FunctionX(XLL_DOUBLEX, _T("?test"), _T("test"))
    .Arg(XLL_DOUBLEX, _T("P"), _T(""))
    .Arg(XLL_DOUBLEX, _T("T"), _T(""))
    .Category(CATEGORY)
    .FunctionHelp(_T(""))
    .Documentation(_T(""))
);

double WINAPI test (double P, double T, bool SI)
{
    #pragma XLLEXPORT 
    double x ;
    //...
    //a lot of work
    //...
    if (SI)
        return x * 0.2388458966275;
    else
        return x;   
}
If I use =test(C1;D1) - function return value
If I use =test(C1;D1;1) - function return #Value error.
How to fix this error? In Excel SDK is it possible...
Coordinator
Aug 22, 2013 at 3:54 PM
You need another .Arg(XLL_BOOLX, ...). If you lie to Excel about the argument stack, there is no way to detect that. Also, use BOOL SI instead of bool SI.
Aug 22, 2013 at 5:42 PM
Edited Aug 22, 2013 at 5:43 PM
keithalewis wrote:
If you lie to Excel about the argument stack, there is no way to detect that.
Detect what? Errors?
Coordinator
Aug 22, 2013 at 5:52 PM
Edited Aug 22, 2013 at 6:20 PM
You told Excel to push two doubles on the call stack and then tried to read the bool that wasn't there.
An earlier version of the library would generate headers based on the AddIn object. If you include them, the compiler will tell you if there is an argument mismatch.
Aug 23, 2013 at 3:40 AM
Then why Excel XLL SDK allow that?
If it's not safe how to make default argument with your library? Thank you!
Coordinator
Aug 23, 2013 at 4:15 AM
How could the XLL SDK prevent it? You can fake default arguments by using LPOPER's and checking to see if xltype is xltypeMissing.
Aug 23, 2013 at 6:11 AM
Edited Aug 23, 2013 at 6:15 AM
Well I try make fake argument with LPOPER's but I'm out of luck:
static AddInX test(
    FunctionX(XLL_DOUBLEX, _T("?test"), _T("test"))
    .Arg(XLL_DOUBLEX, _T("P"), _T(""))
    .Arg(XLL_DOUBLEX, _T("T"), _T(""))
    .Arg(XLL_LPOPERX, _T("S"), _T(""))
    .Category(CATEGORY)
    .FunctionHelp(_T(""))
    .Documentation(_T(""))
);

double WINAPI test(double P, double T, OPERX SI)
{
    #pragma XLLEXPORT 
    double x ;
    //...
    //a lot of work
     //...  
    if (SI.xltype == xltypeMissing)
    {
        return x;
    }
    else if (SI.xltype == xltypeBool)
    {
        if (SI.val.xbool)
        {
            return x * 0.2388458966275;
        }
        else
        {
            return x;
        }
    }
    else if(SI.xltype == xltypeInt)
    {
        if ((BOOL)SI.val.num)
        {
            return x * 0.2388458966275;
        }
        else
        {
            return x;
        }
    }
    else
    {
        return x;
    }
}
And in Excel 2007 I try this:
                            A|  B|      C|  D|
1 =test(B1;C1) | 1| 234| | - Return Value 2942,828331
2 =test(B2;C2;D2) | 1| 234| 0| - Return Value 2942,828331
3 =test(B3;C3;D3) | 1| 234| 1| - Return Value 2942,828331

Now I'm confused why my if's not work or something I wrote incorrect?
Coordinator
Aug 23, 2013 at 6:14 AM
Edited Aug 23, 2013 at 6:21 AM
If Arg says XLL_LPOPERX, then the function should use LPOPERX, a pointer to an OPER.
Aug 23, 2013 at 6:28 AM
keithalewis wrote:
If Arg says XLL_LPOPERX, then the function should use LPOPERX, a pointer to an OPER.
I try this:
double WINAPI PT_H (double P, double T, LPOPERX S)
{
    #pragma XLLEXPORT   
    OPERX SI = * S;
But problem the same...
Coordinator
Aug 23, 2013 at 6:43 AM
Looks fine to me. Send me a complete example that exhibits this behavior and I'll take a look.
Aug 23, 2013 at 6:50 AM
keithalewis wrote:
Looks fine to me. Send me a complete example that exhibits this behavior and I'll take a look.
Please look at your e-mail inbox. Thank you!
Coordinator
Aug 23, 2013 at 6:51 AM

Please send me a text file.

Aug 23, 2013 at 7:04 AM
keithalewis wrote:
Please send me a text file.
I send txt file with this function. Thank you!
Coordinator
Aug 23, 2013 at 7:18 AM

Your example is not self-contained. Attached is a file you should be able to compile and run. Let me know if it does not work on your machine.

Aug 23, 2013 at 7:27 AM
keithalewis wrote:
Your example is not self-contained. Attached is a file you should be able to compile and run. Let me know if it does not work on your machine.
Ok. I send you my .cpp and .h files. Please look at PT_H function instead of test in example above. Thank you!