Jump to content
MatthiasPeters

why can't I create a VarArray of type "varInt64"?

Recommended Posts

Hi,

in a wrapper class I need to convert arrays into variables of type "OleVariant". Today I discovered that sometimes my integer values are bigger than 32-bit integers, so instead of

OleOutput := VarArrayCreate([0, iMaxIndex], VarInteger);

I wanted to write:

OleOutput := VarArrayCreate([0, iMaxIndex], VarInt64);

But this gives me an error message when executed.

My only workaround so far was to use the general VarVariant datatype:

OleOutput := VarArrayCreate([0, iMaxIndex], VarVariant); 

but I am a little worried if this is not slowing everything down.

 

Can anybody explain me, why the use of datatype VarInt64 is not allowed in VarArrays?

 

Thank you!

 

 

Share this post


Link to post

Currency is not a floating-point type - it is a fixed-point type that essentially is an Int64 where the last 4 digits represent the decimal part

 

Also, I would assume the code that you pass that OleVariant cannot handle this - you can very well create a Variant array of Int64 - the following code works fine for me:

 

  var vals := VarArrayCreate([0, 9], VarInt64);
  for var i := 0 to 9 do
    vals[i] := High(int64);

  for var i := VarArrayLowBound(vals, 1) to VarArrayHighBound(vals, 1) do
    Writeln(vals[i]);

 

Also, can we please burn that clearly wrong AI answer?

 

If one would actually spend 30 seconds operating Google and look into the Microsoft documentation, they would find this:

https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-oaut/8c78fede-6f6c-4822-a5f8-0fcbbc8d8242, which clearly mentions support for 8 byte integer.

Edited by Stefan Glienke
  • Like 2

Share this post


Link to post
52 minutes ago, Stefan Glienke said:

...you can very well create a Variant array of Int64 - the following code works fine for me:

 


  var vals := VarArrayCreate([0, 9], VarInt64);
  for var i := 0 to 9 do
    vals[i] := High(int64);

  for var i := VarArrayLowBound(vals, 1) to VarArrayHighBound(vals, 1) do
    Writeln(vals[i]);

 

 

I had to change it to compile in Delphi 10.2 but it does not run without error:

 

image.png.b68e1ad826a9986a08ddd12a26107048.png

 

 vals:variant;
 i:integer;
begin
 vals := VarArrayCreate([0, 9], VarInt64);		// <-      error
  for i := 0 to 9 do
    vals[i] := High(int64);

  for i := VarArrayLowBound(vals, 1) to VarArrayHighBound(vals, 1) do
    Writeln(vals[i]);
end;

Something wrong with the code?

 

  • Thanks 1

Share this post


Link to post

Looks like they have only been marked as being valid base elements in Delphi 13. The code flow is:

     VarArrayCreate defined in System.Variants.pas calls VarTypeIsValidArrayType defined in System.VarUtils.pas which checks against the CVarTypeToElementInfo array of which the last two elements are shown below:

// In Delphi 12.3

    { varInt64/vt_i8           $14 }
    (ValidBase: False; ValidElement:  True; Size:  8; Flags: ARR_NONE),
    { varUInt64/vt_ui8         $15 }
    (ValidBase: False; ValidElement:  True; Size:  8; Flags: ARR_NONE));


// In Delphi 13

    { varInt64/vt_i8           $14 }
    (ValidBase:  True; ValidElement:  True; Size:  8; Flags: ARR_NONE),
    { varUInt64/vt_ui8         $15 }
    (ValidBase:  True; ValidElement:  True; Size:  8; Flags: ARR_NONE));

Did not check if other things changed as well to now support them. 

Edited by Brian Evans
  • Thanks 1

Share this post


Link to post

Well, for once, we can be glad that typed consts are just global variables write-protected by the compiler (but not by being placed in the data segment) - so you can simply do this:

  PBoolean(@CVarTypeToElementInfo[varInt64].ValidBase)^ := True;
  PBoolean(@CVarTypeToElementInfo[varUInt64].ValidBase)^ := True;

 

Edited by Stefan Glienke
  • Like 2

Share this post


Link to post

Hi Everyone,

thank you for your replies on my question. I forgot to say that I use Delphi 12. Now that I know this is fixed in Delphi 13 this is a good time for me to check if I am allowed to upgrade for free.

Anyway: Stefans "Hack" seems to work perfectly. I hope there are no sideeffects though...

 

 

 

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×