Search code examples
delphifiremonkey

Function returning false on array and integer compare?


I am trying to check if an integer value exists in an array, but the function is returning false even if the integer exists in the array.

This is code I am using. The error is 404 but it is returning false:

const
  cErrors: array [0 .. 3] of integer = (401, 404, 409, 411);

function isInError(const error: integer; const sArray: array of integer): Boolean;
var
  i: integer;
begin
  Result := False;
  for i in sArray do
    if sArray[i] = error then
      Result := True;
  ShowMessage(error.ToString);// it's returining false always and this showmessage is just verify the error code
end;

and I'm calling it like this:

if (isInError(sPdf.LastErrorCode, cErrors)) then
  ShowMessage(sPdf.LastErrorCode.toString);

Solution

  • This looks wrong:

      for i in sArray do
        if sArray[i] = error then
    

    The for .. in already extracts values from the array.

      for i in sArray do
        if i = error then
    

    Also turn on range checking to make sure you are not going outside of array boundaries.