Recent

Author Topic: Why can't a TMemo Text property be set directly?  (Read 4485 times)

vfclists

  • Hero Member
  • *****
  • Posts: 1165
    • HowTos Considered Harmful?
Why can't a TMemo Text property be set directly?
« on: July 14, 2014, 05:15:06 am »
Is there some reason why although it is possible to set the Text property  of a TStringList from the Lines.Text property of a TMemo, the converse isn't possible, or doesn't seem to work..

This works

Code: [Select]
valueList .Text := actvFrame.mmoParametersAvailable.Lines.Text
This does not work
Code: [Select]
actvFrame.mmoParametersAvailable.Lines.Text := valueList.Text;
UPDATED: Corrected to match howardpc's comment
« Last Edit: July 14, 2014, 10:38:39 am by vfclists »
Lazarus 3.0/FPC 3.2.2

kpeters58

  • Sr. Member
  • ****
  • Posts: 267
Re: Why can't a TMemo Text property be set directly?
« Reply #1 on: July 14, 2014, 06:58:34 am »
It surely works just fine. When in doubt, always test with the simplest possible case. If necessary, create a special mini project for this.

I suspect your issue lies elsewhere in your code....


Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
var
  slist: TStringList;

begin
  slist := TStringList.Create;
  try
    slist.CommaText := 'The,quick,brown,fox,jumped,over,the,lazy,dog.';
    memo1.Text      := slist.Text;
  finally
    if Assigned(slist) then
      slist.Free;
  end;
end;
Lazarus 2.0.4/FPC 3.0.4/Win 64

jdlinke

  • Jr. Member
  • **
  • Posts: 62
  • Just old me
Re: Why can't a TMemo Text property be set directly?
« Reply #2 on: July 14, 2014, 06:58:47 am »
From my understanding, TStrings is an abstract class, and shouldn't be used for this purpose.

Use a TStringList instead, as I've shown in the attached example.
Lazarus 1.2.4 32-bit version on Windows 8.1 64-bit, Windows 7 64-bit, and Windows XP 32-bit

Currently developing TimberLog and the VirtualDBScroll Component Package

jdlinke

  • Jr. Member
  • **
  • Posts: 62
  • Just old me
Re: Why can't a TMemo Text property be set directly?
« Reply #3 on: July 14, 2014, 07:02:47 am »
It surely works just fine. When in doubt, always test with the simplest possible case. If necessary, create a special mini project for this.

I have *dozens* of mini projects from exactly this sort of situation.  :D

So much easier to test something all on its own, and then implement it into a larger project.
Lazarus 1.2.4 32-bit version on Windows 8.1 64-bit, Windows 7 64-bit, and Windows XP 32-bit

Currently developing TimberLog and the VirtualDBScroll Component Package

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Why can't a TMemo Text property be set directly?
« Reply #4 on: July 14, 2014, 09:23:38 am »
From my understanding, TStrings is an abstract class, and shouldn't be used for this purpose.

I think you are confusing issues with that statement.
The TMemo.Lines property is of type TStrings, and this property is not used or mentioned by the OP.
Lines is declared in TMemo as an abstract ancestor type (TStrings) precisely so that you can assign to it any type of TStrings descendant (including TStringList).

TMemo.Text is of type TCaption which is an alias for string. Likewise the TStringList.CommaText and .Text properties are of type string. So these properties are type-compatible, as kpeters58's code shows.

Perhaps vfclists' typo ("the Text property  TString") in the first posting set you off in the wrong directiion.
« Last Edit: July 14, 2014, 09:32:17 am by howardpc »

vfclists

  • Hero Member
  • *****
  • Posts: 1165
    • HowTos Considered Harmful?
Re: Why can't a TMemo Text property be set directly?
« Reply #5 on: July 14, 2014, 11:54:38 am »
It was working earlier.

I suspect it must be the result of some memory corruption somewhere, or some error in code generation.

I will try the same code later on a newer version of Lazarus and FPC, the current system is Lazarus 1.0.7 fixes and FPC 2.6.0
Lazarus 3.0/FPC 3.2.2

 

TinyPortal © 2005-2018