Recent

Author Topic: I thought .text in a TEdit was a VARIABLE?  (Read 8368 times)

Elmug

  • Hero Member
  • *****
  • Posts: 849
I thought .text in a TEdit was a VARIABLE?
« on: July 18, 2011, 04:58:39 am »
In FormX I have a button called BtnTEST, to delete some characters from the TEdit panel, which I called EntryDisplay, when clicked.

Using the string function Delete, I did this first:

procedure TFormX.BtnTEST(Sender: TObject);
begin
    Delete ( EntryDisplay.Text , 1 , 3) ;
  end;

 
Got this error message:
Can't take the address of constant expressions

Then I did this, and it worked:

procedure TFormX.BtnTEST(Sender: TObject);
var
StringVar: string;
begin
    StringVar:= EntryDisplay.Text;
    Delete (StringVar , 1 , 3) ;
    EntryDisplay.Text := StringVar;
end;   


I have three questions:

1. Is there a way to achieve the result, without having to use a var?

2. Or, is using the var as I did, about the best way?

Reason for asking is that using a var (StringVar in this case) kind of seemed unnecessary to me.

Also,
3. Why is the .Text property considered a Constant, if it actually can be varied?

Thanks.
 

felipemdc

  • Administrator
  • Hero Member
  • *
  • Posts: 3538
Re: I thought .text in a TEdit was a VARIABLE?
« Reply #1 on: July 18, 2011, 08:18:09 am »
1. Is there a way to achieve the result, without having to use a var?

No

Quote
2. Or, is using the var as I did, about the best way?

It is the best way.

Quote
Reason for asking is that using a var (StringVar in this case) kind of seemed unnecessary to me.

Also,
3. Why is the .Text property considered a Constant, if it actually can be varied?

It is not unnecessary. See the declaration of Delete:

http://www.freepascal.org/docs-html/rtl/system/delete.html

It's first parameter is of kind var, therefore it expects a string variable. SomeControl.Text is not a string variable, it is a property of type string. Properties run code to execute the read / write access, while string variables are real addresses pointing to data.

The extra variable would not be required if the parameter was not of type "var"

Elmug

  • Hero Member
  • *****
  • Posts: 849
Re: I thought .text in a TEdit was a VARIABLE?
« Reply #2 on: July 18, 2011, 09:57:55 am »
1. Is there a way to achieve the result, without having to use a var?

No

Quote
2. Or, is using the var as I did, about the best way?

It is the best way.

Quote
Reason for asking is that using a var (StringVar in this case) kind of seemed unnecessary to me.

Also,
3. Why is the .Text property considered a Constant, if it actually can be varied?

It is not unnecessary. See the declaration of Delete:

http://www.freepascal.org/docs-html/rtl/system/delete.html

It's first parameter is of kind var, therefore it expects a string variable. SomeControl.Text is not a string variable, it is a property of type string. Properties run code to execute the read / write access, while string variables are real addresses pointing to data.

The extra variable would not be required if the parameter was not of type "var"

Thanks for your great reply, Felipe. Superficially I follow and make sense, but I do need to understand this fundamental relationships, like X.Text being a PROPERTY and, I believe, understand what the error message said about it being a constant expression. I guess also that this has something to do of variables being in the Stack and Types in the heap (or something like that) that I read a few days ago in my effort of learning Lazarus/Free-Pascal.

But I am clear and resolved, specially since you say it's OK the way I figured it out.

Thanks!

Zoran

  • Hero Member
  • *****
  • Posts: 1988
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: I thought .text in a TEdit was a VARIABLE?
« Reply #3 on: July 18, 2011, 10:35:45 am »
Properties cannot be passed as var parameters.

Properties are not variables. Although they can be accessed as if they were variables, reading or writing a property actually only triggers specialized procedure/function – to understand better what properties actually are, see: http://www.freepascal.org/docs-html/ref/refse34.html
Swan, ZX Spectrum emulator https://github.com/zoran-vucenovic/swan

felipemdc

  • Administrator
  • Hero Member
  • *
  • Posts: 3538
Re: I thought .text in a TEdit was a VARIABLE?
« Reply #4 on: July 18, 2011, 10:52:42 am »
Thanks for your great reply, Felipe. Superficially I follow and make sense, but I do need to understand this fundamental relationships, like X.Text being a PROPERTY and, I believe, understand what the error message said about it being a constant expression. I guess also that this has something to do of variables being in the Stack and Types in the heap (or something like that) that I read a few days ago in my effort of learning Lazarus/Free-Pascal.

No, it's not at all related to heap versus stack.

The problem is that a variable might be in the stack or in the heap, but regardless of that, it is somewhere in the RAM which can be addresses and accessed by the processor.

A property on the other hand is getter/setter pair of functions. It is not a value with an address in the RAM. Actually the property is also in the RAM, but the pointer goes to the property method, not to the data itself.

Your code:

    Delete ( EntryDisplay.Text , 1 , 3) ;

Is converted by the compiler into this code:

    TemporaryVar := EntryDisplay.GetText();
    Delete ( TemporaryVar, 1 , 3) ;

At this point the compiler does not suppose that you want to use both directions of the property. The result of a method cannot be directly modified because it is a temporary variable. I guess that the compiler complains that the temporary variable is read-only, that's why it says that you passed a constant expression. The temporary variable is read only for safety, because anything you write to it will be lost and it might even get freed shortly.

But yes, the compiler could theorically generate the code for calling first the getter and then the setter automatically, but it is a design choice that it doesn't.

Elmug

  • Hero Member
  • *****
  • Posts: 849
Re: I thought .text in a TEdit was a VARIABLE?
« Reply #5 on: July 18, 2011, 01:02:30 pm »
Thanks, Felipe.

You are very good at explaining the insides of these issues, and that makes one comfortable that deeper waters are swimmable. :o

I got what you are saying, but will keep checking on it as a reference, till I can really walk it, or swim on it.

eny

  • Hero Member
  • *****
  • Posts: 1658
Re: I thought .text in a TEdit was a VARIABLE?
« Reply #6 on: July 19, 2011, 10:18:49 pm »
Code: Pascal  [Select][+][-]
  1.   EntryDisplay.Text := copy(EntryDisplay.Text,3,length(EntryDisplay.Text)-2)
All posts based on: Win11; Lazarus 4_4  (x64) 12-02-2026 (unless specified otherwise...)

 

TinyPortal © 2005-2018