Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I get "E2026 Constant expression expected"?

I have to move a file in the system32 folder, I used this code:

//-----------FUNCTION----------------
function GetWindowsSystemDir(): String;
var
  vlBuff: Array[0..MAX_PATH-1] of Char;
begin
  getSystemDirectory(vlBuff, MAX_PATH);
  Result := vlBuff;
end;
//-----------------------------------

const
  SMyFile = GetWindowsSystemDir+'\intructions.txt'; //error here, line 87
var
  S: TStringList;
begin
  S := TStringList.Create;
  try
    S.Add('intructions');
    S.SaveToFile(SMyFile);
  finally
    S.Free;
  end;
end;

gives me error when compiling:

[DCC Error] Unit1.pas(87): E2026 Constant expression expected

Thanks.

like image 603
Giacomo King Patermo Avatar asked Jun 21 '12 17:06

Giacomo King Patermo


1 Answers

As the compiler error message indicates, it expects a constant expression where you're initializing the const. But you're calling a function there, and the compiler won't evaluate it at compile time.

Declare a variable instead, and assign it inside the regular begin-end block of your code:

var
  SMyFile: string;
  S: TStringList;
begin
  S := TStringList.Create;
  try
    S.Add('intructions');
    SMyFile := GetWindowsSystemDir+'\intructions.txt';
    S.SaveToFile(SMyFile);
  finally
    S.Free;
  end;
end;
like image 59
Rob Kennedy Avatar answered Nov 18 '22 15:11

Rob Kennedy