Why am I getting the expected expression "E2026 Constant"?

I need to move the file to 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 a compilation error:

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

Thank.

+5
source share
1 answer

As the compiler error message indicates, it expects a constant expression in which you initialize const. But you call the function there, and the compiler will not evaluate it at compile time.

Declare a variable instead and assign it inside the regular start 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;
+14
source

All Articles