Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi Constant Expression Expected

I get a "Constant expression expected" error with the following code:

TBoard is defined as:

  TBoard = class
    field: array[1..5,1..5] of Integer;

    function check(const x, y: Integer): Integer;
    function addShip(x, y, size, dir: Integer): Integer;
    function attack(const x, y: Integer): Integer;
  end;

I get the error on the marked line:

function TBoard.attack(const x, y: Integer): Integer;
begin
  Result := Self.check(x, y);
  case Result of
  0:
    Self.field[x, y] := 1;
    Exit; // error: constant expression expected
  else Exit;
  end;
end;

Does somebody know what is going on?
Thanks in advance!

like image 493
Ricardo Boss Avatar asked Jan 28 '26 05:01

Ricardo Boss


1 Answers

You are simply missing begin and end inside the case statement, so change your function to

function TBoard.attack(const x, y: Integer): Integer;
begin
  Result := Self.check(x, y);
  case Result of
  0:
    begin
      Self.field[x, y] := 1;
      Exit; 
    end
  else Exit;
  end;
end;

However, if this is your full code, you could simplify it very much, you don't need all these exits and also not the case-statement:

function TBoard.attack(const x, y: Integer): Integer;
begin
  Result := Self.check(x, y);
  if Result = 0 then
    Self.field[x, y] := 1;
end;
like image 136
Sebastian Proske Avatar answered Jan 31 '26 00:01

Sebastian Proske



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!