Recent

Author Topic: [SOLVED]why can't use Memo1.Text.Length ?  (Read 2884 times)

tomitomy

  • Full Member
  • ***
  • Posts: 223
[SOLVED]why can't use Memo1.Text.Length ?
« on: November 08, 2017, 01:31:20 pm »
Hi, I have a problem, when I use Memo1.Text, I can't use the methods of string, I don't know why.

The following code can not be compiled:
Code: Pascal  [Select]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.   Caption := Memo1.Text.Length.ToString;
  4. end;

Is this a bug?

Lazarus: 1.8.0 RC4 GTK2
OS: Linux Mint Mate 64-bit
« Last Edit: November 08, 2017, 01:41:57 pm by tomitomy »

howardpc

  • Hero Member
  • *****
  • Posts: 3201
Re: why can't use Memo1.Text.Length ?
« Reply #1 on: November 08, 2017, 01:36:32 pm »
It's not a bug.
Text is a property of TMemo, not a simple variable.
If you copy Text to a local string variable, you can use any of the string helper functions on the local variable.

tomitomy

  • Full Member
  • ***
  • Posts: 223
Re: why can't use Memo1.Text.Length ?
« Reply #2 on: November 08, 2017, 01:41:39 pm »
I understand, Thank you howardpc!  :)

Noodly

  • Jr. Member
  • **
  • Posts: 70
Re: [SOLVED]why can't use Memo1.Text.Length ?
« Reply #3 on: November 08, 2017, 01:58:40 pm »
This would also work:

Code: [Select]
procedure TForm1.FormCreate(Sender: TObject);
begin
  Caption := IntToStr(length(Memo1.Text));
end;
Windows 10 Home, Lazarus 2.02 (svn 60954), FPC 3.04

tomitomy

  • Full Member
  • ***
  • Posts: 223
Re: [SOLVED]why can't use Memo1.Text.Length ?
« Reply #4 on: November 08, 2017, 02:03:35 pm »
This would also work:

Code: [Select]
procedure TForm1.FormCreate(Sender: TObject);
begin
  Caption := IntToStr(length(Memo1.Text));
end;

Thank you Noodly :), I have another way:

Code: Pascal  [Select]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.   Caption := string(Memo1.Text).Length.ToString;
  4. end;

ASerge

  • Hero Member
  • *****
  • Posts: 1422
Re: [SOLVED]why can't use Memo1.Text.Length ?
« Reply #5 on: November 09, 2017, 09:14:17 pm »
The types of TCaption and string are different. Operators from type helper for string are not applicable to type TCaption.

tomitomy

  • Full Member
  • ***
  • Posts: 223
Re: [SOLVED]why can't use Memo1.Text.Length ?
« Reply #6 on: November 10, 2017, 05:54:27 am »
The types of TCaption and string are different. Operators from type helper for string are not applicable to type TCaption.

I don't understand, what's the difference? Can I use "string(Memo1.Text).Length.ToString" This usage?

howardpc

  • Hero Member
  • *****
  • Posts: 3201
Re: [SOLVED]why can't use Memo1.Text.Length ?
« Reply #7 on: November 10, 2017, 09:46:05 am »
As ASerge writes, it is a difference of type.
Pascal is a very strongly typed language, so helpers defined for one type are not magically defined for another type, even if the types are simply aliases for each other.
TCaption is an alias for TTranslateType, which is an alias for String.
So the cast String(Memo1.Text) is quite safe, since fundamentally they are the "same" type.
But to the compiler they are distinct types (notwithstanding their equivalence), and a helper defined for String is not thereby a helper for TCaption.

tomitomy

  • Full Member
  • ***
  • Posts: 223
Re: [SOLVED]why can't use Memo1.Text.Length ?
« Reply #8 on: November 10, 2017, 11:38:08 am »
Thank you howardpc, I understand.  :)

wp

  • Hero Member
  • *****
  • Posts: 6467
Re: [SOLVED]why can't use Memo1.Text.Length ?
« Reply #9 on: November 10, 2017, 11:43:37 am »
Why are so many people up to the "modern" syntax "Memo1.Text.Length.ToString" and refuse to apply the "old-fashioned" Syntax "IntToStr(length(Memo1.Text))"? Having to type 4 brackets instead of 2 points?
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

GetMem

  • Hero Member
  • *****
  • Posts: 3515
Re: [SOLVED]why can't use Memo1.Text.Length ?
« Reply #10 on: November 10, 2017, 11:48:00 am »
@wp
Quote
Why are so many people up to the "modern" syntax "Memo1.Text.Length.ToString"
Bad habit inherited from c#.

tomitomy

  • Full Member
  • ***
  • Posts: 223
Re: [SOLVED]why can't use Memo1.Text.Length ?
« Reply #11 on: November 10, 2017, 01:06:10 pm »
I think it is more natural to read it this way, and there will be syntax prompt after entering the dot. I am unfamiliar with many function names, and I hope the syntax prompt can be a bit more powerful. like this:

Code: [Select]
Something.ABCD
prompt->  ABCD
          ABCD+++
          A++B++C++D

Thaddy

  • Hero Member
  • *****
  • Posts: 9284
Re: [SOLVED]why can't use Memo1.Text.Length ?
« Reply #12 on: November 10, 2017, 01:08:19 pm »
@wp
Quote
Why are so many people up to the "modern" syntax "Memo1.Text.Length.ToString"
Bad habit inherited from c#.
c#. Java... Imho not a bad habit, but a good thing...

Aside, on topic, ToString should work, but is an AnsiString, not UTF8. There are no type helpers yet for UTF8.
« Last Edit: November 10, 2017, 01:12:23 pm by Thaddy »
also related to equus asinus.

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1250
Re: [SOLVED]why can't use Memo1.Text.Length ?
« Reply #13 on: November 11, 2017, 08:24:55 pm »
Why are so many people up to the "modern" syntax "Memo1.Text.Length.ToString" and refuse to apply the "old-fashioned" Syntax "IntToStr(length(Memo1.Text))"? Having to type 4 brackets instead of 2 points?

lol  As I get older, I find myself saying this about so many different things :-)

But as for the string helpers themselves.  Wow, I had no idea we had those.  Coming back to the forum after a few years away is definitely making me feel my age :-)  I've already had more than a few double takes seeing code like s += 'This would never have worked in my day'; and I knew about the operator helpers...
« Last Edit: November 11, 2017, 08:26:34 pm by Mike.Cornflake »
Lazarus Trunk/FPC Trunk on Windows [7, 10]
  Have you tried searching this forum or the wiki?:   http://wiki.lazarus.freepascal.org/Alternative_Main_Page
  BOOKS! (Free and otherwise): http://wiki.lazarus.freepascal.org/Pascal_and_Lazarus_Books_and_Magazines