Siêu thị PDFTải ngay đi em, trời tối mất

Thư viện tri thức trực tuyến

Kho tài liệu với 50,000+ tài liệu học thuật

© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

Excel Add-in Development in C/C++ Applications in Finance phần 5 pptx
MIỄN PHÍ
Số trang
39
Kích thước
388.9 KB
Định dạng
PDF
Lượt xem
871

Excel Add-in Development in C/C++ Applications in Finance phần 5 pptx

Nội dung xem thử

Mô tả chi tiết

140 Excel Add-in Development in C/C++

return false;

b = (ret_val.val._bool != 0);

return true;

}

Using the cpp_xloper class the conversion would look like this:

cpp_xloper Oper;

// Some code that sets Oper's value...

bool result = (bool)Oper;

The code for the overloaded conversion operator (bool) is:

cpp_xloper::operator bool(void)

{

bool b;

if(coerce_to_bool(&m_Op, b))

return b;

return false;

}

What the memory considerations are

None (unless the 10 bytes for the xloper itself are dynamically allocated), as the integer

_bool is contained entirely within the xloper.

How you can avoid using it

Declare functions as taking int arguments and/or returning ints: Excel will do the

necessary conversions.

6.8.5 Worksheet error value: xltypeErr

When you will encounter it

This xloper type is used by Excel for all error values passed from worksheets to a

DLL. When you want your DLL code to be called even if one of the inputs evaluates to

an error (such as range with invalid references – #REF!), you should declare arguments

as xlopers. Otherwise Excel will intercept the error and fail the function call before the

DLL code is even reached.

This xloper type is returned by most of the C API functions when they fail to

complete successfully. DLL functions accessed via VB that accept Variant arguments, or

Passing Data between Excel and the DLL 141

arrays of Variants, may need to convert between the Variant representation of Excel errors

and the C API error codes. This is discussed in section 3.6.11 Variant types that Excel

can pass to VB functions on page 59.

When you need to create it

Excel’s error codes provide a very well understood way of communicating problems to

the worksheet, and are therefore very useful. They have the added benefit of propagating

through to dependent cells. It’s a good idea to declare fallible worksheet functions as

returning xlopers so that errors can be returned, in addition to the desired output type.

You might even want to pass an error code into a C API function, although this

is unlikely.

How you create an instance of it

An example of code to populate an xloper of this type is:

void set_to_err(xloper *p_op, WORD e)

{

if(!p_op) return;

switch(e)

{

case xlerrNull:

case xlerrDiv0:

case xlerrValue:

case xlerrRef:

case xlerrName:

case xlerrNum:

case xlerrNA:

p_op->xltype = xltypeErr;

p_op->val.err = e;

break;

default:

p_op->xltype = xltypeMissing; // not a valid error code

}

}

Using the cpp_xloper class, creation can look like any of these:

WORD x, y, z;

//...

cpp_xloper Oper1(x); // creates an xltypeErr xloper, value = x

cpp_xloper Oper2 = y; // creates an xltypeErr xloper, value = y

cpp_xloper Oper3; // creates an xloper of undefined type

// Change the type of Oper3 to xltypeErr, value = z, using the

// overloaded operator =

Oper3 = z;

// Create xltypeErr=z using copy constructor

cpp_xloper Oper4 = Oper3;

142 Excel Add-in Development in C/C++

The code for the xltypeErr constructor is:

cpp_xloper::cpp_xloper(WORD e)

{

Clear();

set_to_err(&m_Op, e);

}

The code for the overloaded conversion operator ‘=’ is:

void cpp_xloper::operator=(WORD e)

{

Free();

set_to_err(&m_Op, e);

}

How you convert it to a C/C++ data type

It is unlikely that you will need to convert an error type to another data type. If you do

need the numeric error value, it is obtained from the err element of the xloper’s val

union.

What the memory considerations are

None (unless the 10 bytes for the xloper itself are dynamically allocated), as the integer

err is contained entirely within the xloper.

How you can avoid using it

If you want to write worksheet functions that can trap and generate errors, you can’t.

6.8.6 Excel internal integer: xltypeInt

When you will encounter it

This xloper type is NEVER passed by Excel from worksheets to a DLL. Some of the

C API functions might return this type.

When you need to create it

A number of Excel’s own functions take integer arguments and when calling them from

within the DLL this data type should be used. (Excel will try to convert the xltypeNum

type, if that is passed instead.) It can be used to pass integers, within its range, back to

Excel, especially in those cases where you might also want to return, say, an Excel error.

Again, the xltypeNum type can also be used for this and using xltypeInt does not

deliver any advantage in this case.

Tải ngay đi em, còn do dự, trời tối mất!