I’m porting a Delphi project to 64 bits and I have a problem with a line of code which has the IN operator.
The compiler raise this error
E2010 Incompatible types: ‘Integer’ and ‘Int64’
I wrote this sample app to replicate the problem.
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils;
Var
I : Integer;
L : Array of string;
begin
try
if I in [0, High(L)] then
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
readln;
end.
This code works ok in 32 bits, but why doesn’t it compile in Delphi XE2 64 bits? How I can fix this issue?
*UPDATE *
It seems which my post caused a lot of confusion (sorry for that) , just for explain the original code which i’m porting is more complex, and I just wrote this code as a sample to illustrate the issue. the original code uses an in operator to check if a value (minor than 255) belongs to a group of values (all minor or equal to 255) like so
i in [0,1,3,50,60,70,80,127,High(LArray)]
This code can’t be compiled because the
Highfunction is returning a 8 byte value, which is not a ordinal value. and the In operator can be only used in sets with ordinal values.FYI, the size of the results returned by the High function is different depending of the parameter passed as argument.
Check this sample
Finally, you can fix your code casting the result of High function to integer.
UPDATE
Check the additional info provided by David and remember to be very careful when you use the
inoperator to check the membership of a value in set with variable values. Theinoperator only checks the least significant byte of each element (in delphi 32 bits).Check this sample
This return true because the low byte of 257 is 1.
And in Delphi 64 bits, the values greater than 255 are removed of the set. So this code
will return false because is equivalent to