views:

154

answers:

3

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

+2  A: 

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.

Mason Wheeler
Yes you are correct, this was because of name confusion
Bharat
@Bharat: then please mark this answer as 'accept'.
Jeroen Pluimers
@mason wheeler: please see my edit
Omair Iqbal
@Jeroen, Bharat cannot mark this as accepted because it's not his question. Furthermore, the name confusion is not with the System.Text type but with the TForm.Text property, as Bharat suggests in his answer.
Rob Kennedy
@Omair: Yeah, Bharat's right. It's a name confusion, but I was mistaken about *what* it was getting confused with. I wasn't aware that TForm actually had a Text property; I thought it used Caption. Turns out both are declared **protected** on TControl and both resolve to the same thing.
Mason Wheeler
please see my edit2
Omair Iqbal
+4  A: 

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.

Bharat
and what about when i declare it in the unit's var section would compiler still consider it Form1.Text
Omair Iqbal
If you declare it in the Timer1Timer(Sender: TObject), then compiler will treat as Text (string) but not as Form1.Text, as it was declared inside the scope of timer method
Bharat
Anything outside the Timer method, it will treat as Form1.Text
Bharat
Omair, the Text property declared in an ancestor of your class has *closer scope* than the Text variable you declare *outside* the function, but *further scope* than the one you declare *inside* the function. The compiler binds to whatever is closest. You can give hints about what you want by *qualifying* the name. Say `Self.Text` for the property, or `UnitName.Text` for the global variable, or `System.Text` for the type.
Rob Kennedy
please see my edit2
Omair Iqbal
@Omair. Scope, Scope, Scope! When you are inside an Object's method, you have an implicit scope to `Self`, so if `Text` is defined for the `TForm1` class, inside any of `TForm1` methods, `Text` will resolve as `Self.Text` (i.e. your `Form1.Text`) unless it is masked by a more local scoping like a local variable `Text` or inside a `with`.
François
thanks everyone
Omair Iqbal
@François:good explanation now my doubts are almost completly clear ,you use the word 'implict scope' does implict mean indirect and is implict an OO terminology (i have not realy learned OO yet so far havent really felt the need to learn it,is OO important in your opinion?)
Omair Iqbal
@Omair. I used implicit in the common meaning (not a reserved word), i.e. this scope is not explicitly written, you cannot see it looking at the code. It's part of the Object model definition in Delphi. And you're right, you **have to learn OOP** with Delphi.
François
+1  A: 

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.

Mason Wheeler