Getting "Object E2197 Constant cannot be passed as var parameter" when passing var parameter

this code works fine:

procedure TForm2.Timer1Timer(Sender: TObject);
var
  Text: string;
begin  SetLength (Text,555);
  GetWindowText (getforegroundwindow, PChar (Text),555);
  Form2.gtListBox1.Items.Add (
    IntToStr (getforegroundwindow) + ': ' + Text);
end;

but when I put

var
  Text: string;

from the Timer1Timer event handler to the block implementation section or `` text: string '' in units of var section I get error: E2197 A constant object cannot be passed as a var parameter

according to the documentation:

This error message appears when you try to send a constant as a var or out parameter to a function or procedure.

but I did not declare the text as permanent, why am I getting this error?
Edit: @mason wheeler: I don't understand why this works:

implementation
{$R *.dfm}
 var 
 char :integer;//first of all why does delphi let me declare variable that is also a type name
procedure TForm2.Button1Click(Sender: TObject);
begin
char:=11; 
showmessage(IntToStr(char));
end;

, , : '' , '', ?
Edit2: , , . , delphi , :

 with
      form1 do
       text := 'blahblahblah';

delphi, , delphi text := 'blah', form1.text := blah; with form1 do text := 'blah'; / , delphi 2010 -

+3
3

, Text Timer1Timer(Sender: TObject), Form1.Text.

sText .

1:

/ , Form1.Char.

+4

, . "" - , . , , , . - , .

+2

Edit # 2:

This is a standard object-oriented programming convention. When you write a method for an object, the code is implicitly interpreted as being in the area of ​​the object. In other words, each method of an object can be considered as being inside a hidden block with self do.

+1
source

Source: https://habr.com/ru/post/1759902/


All Articles