Reputation: 849
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.
Upvotes: 6
Views: 8762
Reputation: 163287
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;
Upvotes: 15