Recent

Author Topic: Cannot use IsEmpty  (Read 3087 times)

Handoko

  • Hero Member
  • *****
  • Posts: 5159
  • My goal: build my own game engine using Lazarus
Cannot use IsEmpty
« on: January 23, 2018, 04:53:25 pm »
Code: Pascal  [Select][+][-]
  1.   if Edit1.Text.IsEmpty then Exit;

Edit1 is a TEdit, but I got compile time error: "Illegal qualifier".
Why? And how to solve it?
I'm using Lazarus 1.8.0

mrguzgog

  • Jr. Member
  • **
  • Posts: 71
Re: Cannot use IsEmpty
« Reply #1 on: January 23, 2018, 04:57:40 pm »
What about if edit1.text = '' ?

EDIT typo, sorry
« Last Edit: January 23, 2018, 04:59:28 pm by mrguzgog »

wp

  • Hero Member
  • *****
  • Posts: 11923
Re: Cannot use IsEmpty
« Reply #2 on: January 23, 2018, 04:59:42 pm »
Edit1.IsEmpty ?

WooBean

  • Full Member
  • ***
  • Posts: 231
Re: Cannot use IsEmpty
« Reply #3 on: January 23, 2018, 05:02:03 pm »
Try this:

if string(self.Edit1.Text).IsEmpty then .... .

Platforms: Win7/64, Linux Mint Ulyssa/64

Handoko

  • Hero Member
  • *****
  • Posts: 5159
  • My goal: build my own game engine using Lazarus
Re: Cannot use IsEmpty
« Reply #4 on: January 23, 2018, 05:04:39 pm »
The old-fashioned way Edit1.Text = '' works perfectly, but IsEmpty is something new in 2018. :D
I just want to experiment with the new writing style.

Edit1.IsEmpty causes compile time error: Error: identifier idents no member "IsEmpty"

Did I find a bug? IsEmpty is a StringHelper and the result of Edit1.Text is a string.

This one works:
if string(self.Edit1.Text).IsEmpty
But looks ugly, I rather prefer the old-fashioned way.

WooBean

  • Full Member
  • ***
  • Posts: 231
Re: Cannot use IsEmpty
« Reply #5 on: January 23, 2018, 05:08:43 pm »
Well, class type helper member ''IsEmpty" is for string but not for TCaption, so I wrote
"if string(self.Edit1.Text).IsEmpty ...."
« Last Edit: January 23, 2018, 05:10:29 pm by WooBean »
Platforms: Win7/64, Linux Mint Ulyssa/64

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Cannot use IsEmpty
« Reply #6 on: January 23, 2018, 05:10:44 pm »
@ Did I find a bug? IsEmpty is a StringHelper and the result of Edit1.Text is a string.

I don't think so. IMO it's similar reason why you cannot pass property as reference ( procedure xxxx(var ...); ) because it has no adress.
Property Text is in fact function (getter) GetText.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

Handoko

  • Hero Member
  • *****
  • Posts: 5159
  • My goal: build my own game engine using Lazarus
Re: Cannot use IsEmpty
« Reply #7 on: January 23, 2018, 05:13:48 pm »
Both the reasons are reasonable, but sad to know about that I can't use it on some cases.

rvk

  • Hero Member
  • *****
  • Posts: 6171
Re: Cannot use IsEmpty
« Reply #8 on: January 23, 2018, 05:28:00 pm »
Did I find a bug? IsEmpty is a StringHelper and the result of Edit1.Text is a string.

As already said .Text references a getter.
But that shouldn't be a problem because this works too:
Code: Pascal  [Select][+][-]
  1. function Foo: String;
  2. begin
  3.   Result := 'Foo';
  4. end;
  5.  
  6. procedure TForm1.Button1Click(Sender: TObject);
  7. begin
  8.   if Foo.IsEmpty then Exit;
  9. end;

It's the fact the .Text is TCaption which is defined as "type String" (via TTranslateString).
This also doesn't work:
Code: Pascal  [Select][+][-]
  1. type
  2.   Foo = type String;
  3.  
  4. procedure TForm1.Button1Click(Sender: TObject);
  5. begin
  6.   if Foo.IsEmpty then Exit;
  7. end;

But this works:
Code: Pascal  [Select][+][-]
  1. type
  2.   Foo = String;
  3.  
  4. procedure TForm1.Button1Click(Sender: TObject);
  5. begin
  6.   if Foo.IsEmpty then Exit;
  7. end;

So the fact TCaption is defined as "type String" (via TTranslateString) is the problem.
If it was defined just "String" it could work.

Question is, why does it have "type" before String ??

Xor-el

  • Sr. Member
  • ****
  • Posts: 404
Re: Cannot use IsEmpty
« Reply #9 on: January 23, 2018, 05:37:41 pm »
Quote
Question is, why does it have "type" before String ??
Because type TCaption is defined as a new type not as an alias to a string.

So in simple terms, TCaption is not meant to assignment compatible to a string without some cast or some compiler magic (which it does sometimes).

bylaardt

  • Sr. Member
  • ****
  • Posts: 309
Re: Cannot use IsEmpty
« Reply #10 on: January 23, 2018, 05:45:22 pm »
Solution 1:
Code: Pascal  [Select][+][-]
  1. {$MODESWITCH TYPEHELPERS}
  2.  
  3. type        
  4.  
  5.   TMyCaptionhelper = Type Helper for TCaption
  6.   public
  7.      function IsEmpty:boolean;
  8.   end;
  9.  
  10. implementation
  11.                                        
  12. function TMyCaptionhelper.IsEmpty: Boolean;
  13. begin
  14.   Result:=Length(self)=0;
  15. end;    
  16.  
So you can use as you expected.

Solution 2:
Code: Pascal  [Select][+][-]
  1.   String(edit1.text).isempty;
  2.  

Handoko

  • Hero Member
  • *****
  • Posts: 5159
  • My goal: build my own game engine using Lazarus
Re: Cannot use IsEmpty
« Reply #11 on: January 23, 2018, 07:27:45 pm »
Awesome.
I put your solution 1 into MyUtilities unit.

 

TinyPortal © 2005-2018