Reputation: 1838
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 Timer1Timer event handler to
units implementation section or ''text : string'' in the units var section i get error : E2197 Constant object cannot be passed as var parameter
according to documentation :
This error message appears when you try to send a constant as a var or out parameter for a function or procedure.
but i didnt declared text as constant then why am i getting this error?
Edit:@mason wheeler: i do not understand than why does this work:
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;
my first code was not working because i declared text as string ,you say : ''the compiler might think it's a reference to the type and not to the variable'' than why doesnt the compiler think its a reference to the type and not to the variable in this case? i am confused
Edit2: i now understand what was wrong but still have 1 confusion i did'nt use a with statement then why delphi is treating as if i am using:
with
form1 do
text := 'blahblahblah';
this is wrong on the delphi part i mean delphi should not let us do text := 'blah'
but form1.text := blah;
or with form1 do text := 'blah';
do i need to turn on/off some compiler setting(s) i am using delphi 2010 without any ide experts
Upvotes: 3
Views: 4258
Reputation: 84550
With regard to your Edit #2:
That's a standard convention of object-oriented programming. When you're writing a method for an object, the code is implicitly interpreted as being in the scope of the object. In other words, every object method can be considered as being inside a hidden with self do
block.
Upvotes: 1
Reputation: 6866
Actually if you declare Text
in implementation section and use it in Timer1Timer(Sender: TObject)
, compiler will consider Text as Form1.Text
.
Change the name of text as sText and it will work.
Edit 1:
Because there is no property/Field for form like Form1.Char
.
Upvotes: 5
Reputation: 84550
It's probably a name confusion. "Text" is a type name as well, a legacy textfile type. So if you declare the variable in a different scope, the compiler might think it's a reference to the type and not to the variable. Try naming it something else and it should work.
Upvotes: 2