how to use ColumnImportTypeArray in import()


Prompt, please, where it is possible to find examples of use of the Import or Open procedures with the ColumnImportTypeArray parameters. 


    ColumnFormats: array of tColumnImportType;
   MyExcel.Import (Stream, 0,0, #9, ColumnFormats, TEncoding.Default, true);

it is not compiled at me. I can not think how to solve a problem.
This is a Delphi limitation, in Delphi the following code won't compile:

    a: Array of byte;
    b: Array of byte;

Because even when they are both array of bytes, delphi creates a different and incompatible type every time you declare a new array of something.

This is why you can't do:
    ColumnFormats: array of tColumnImportType;

This will create a new type definition that won't be compatible with the one in the method.

But, this restriction doesn't apply to generic arrays, and 2 "TArray<something>" are type compatible, while 2 "array of something" aren't.

In short, define ColumnFormats this way:
    ColumnFormats: TArray<TColumnImportType>;

and it will compile (because it will be type compatible with the TArray<TColumnImportType> that is required)


Ps: Remember that if you don't care about the ColumnImportType, you can set this parameter to nil, no need to define a variable with the array:
   xls.Import (Stream, 0,0, #9, nil, TEncoding.Default, true);

Ps2: Also, if you want you can initialize the array "in place" without defining a variable:
   xls.Import (Stream, 0,0, #9, TArray<TColumnImportType>.Create(TColumnImportType.Skip, TColumnImportType.General), TEncoding.Default, true);

But this is probably going to be less clear. It is probably a good idea to define the array on its own line.

Adrian Gallero2012-09-03 14:58:41

thank you, it works!