Recent

Author Topic: "Can't take the address of constant expressions"  (Read 1535 times)

Jvan

  • Full Member
  • ***
  • Posts: 181
"Can't take the address of constant expressions"
« on: August 03, 2020, 01:41:25 am »
I coded this:

Code: Pascal  [Select][+][-]
  1. procedure TfrmMain.txtMovMntDChange(Sender: TObject);
  2. var
  3.   myfloat: double;
  4. begin
  5.   if not(TryStrToFloat(txtMovMntD.Text, myfloat)) then begin
  6.     SetLength(txtMovMntD.Text, Length(txtMovMntD.Text) - 1);
  7.     ShowMessage('Debe ingresar un monto válido.');
  8.   end;
  9. end;
  10.  

But I got this error message: "Can't take the address of constant expressions".
I'm trying to validate that the user wrote a float number, and remove the last character if not.

Warfley

  • Hero Member
  • *****
  • Posts: 1872
Re: "Can't take the address of constant expressions"
« Reply #1 on: August 03, 2020, 01:50:58 am »
Text is a property, not a variable. SetLength only works on variables. You can get a valua via the property and set a value to it, so one Alternative could be:
Code: Pascal  [Select][+][-]
  1. txtMovMntD.Text := txtMovMntD.Text.Substring(0, txtMovMntD.Text.Length -1);

Jvan

  • Full Member
  • ***
  • Posts: 181
Re: "Can't take the address of constant expressions"
« Reply #2 on: August 03, 2020, 02:01:52 am »
Text is a property, not a variable. SetLength only works on variables. You can get a valua via the property and set a value to it, so one Alternative could be:
Code: Pascal  [Select][+][-]
  1. txtMovMntD.Text := txtMovMntD.Text.Substring(0, txtMovMntD.Text.Length -1);

I have this error message: "Error: Illegal qualifier"

TRon

  • Hero Member
  • *****
  • Posts: 4277
Re: "Can't take the address of constant expressions"
« Reply #3 on: August 03, 2020, 02:06:34 am »
I have this error message: "Error: Illegal qualifier"
In case you have unit sysutils in your uses clause and are using freepascal 3.2.0 or newer then it is because text is of type tcaption(=ttranslationstring), which isn't a string so the helper isn't available I guess.

In that case you could try casting text as a string before the code tries to access the stringhelper.
Today is tomorrow's yesterday.

jamie

  • Hero Member
  • *****
  • Posts: 6869
Re: "Can't take the address of constant expressions"
« Reply #4 on: August 03, 2020, 02:13:48 am »
he wants to simply delete the last character at the end of the string of a TEDIT I would guess..


 For example

 If Not_A_Valid_Number THen
 
      EDIT.TEXT := Copy(EDIT.TEXT, 1, Length(EDIT.TEXT)-1);
The only true wisdom is knowing you know nothing

Jvan

  • Full Member
  • ***
  • Posts: 181
Re: "Can't take the address of constant expressions"
« Reply #5 on: August 03, 2020, 02:22:11 am »
That works. Thanks.

 

TinyPortal © 2005-2018