Recent

Author Topic: TStringList  (Read 13826 times)

T-bear

  • Full Member
  • ***
  • Posts: 160
TStringList
« on: April 18, 2011, 09:39:35 am »
Hi,
I try to load the lines from a stringlist into a Memo, but get the error
unit2.pas(47,51) Error: Incompatible type for arg no. 1: Got "AnsiString", expected "TStrings"
The code i use is something like
Memo1.Lines:=StringList.Text.
--
Anyone has an idea?

Thanks! :D

JimBeam

  • New Member
  • *
  • Posts: 36
Re: TStringList
« Reply #1 on: April 18, 2011, 09:55:31 am »
Try this:
Memo1.Lines := StringList

T-bear

  • Full Member
  • ***
  • Posts: 160
Re: TStringList
« Reply #2 on: April 18, 2011, 10:02:00 am »
And when i want to do the opposite thing:
Stringlist.text:=Memo1.Lines

I have both tried
StringList.text:=Memo1.Lines; ,
StringList.text:=Memo1; ,
StringList:=Memo1.Lines; and
StringList:=Memo1;

Whats wrong with this one?

Thanks! :D
« Last Edit: April 18, 2011, 10:53:33 am by T-bear »

fabienwang

  • Sr. Member
  • ****
  • Posts: 449
  • Lazarus is the best
    • My blog
Re: TStringList
« Reply #3 on: April 18, 2011, 10:56:34 am »
try this:

Code: Pascal  [Select][+][-]
  1. var
  2.   stringlist: TStringList;
  3. begin
  4.   stringlist := TStringList.Create;
  5.   stringlist := memo1.Lines;
I'm using Arch Linux.
Known for: CPickSniff, OpenGrabby
Contributed to: LazPaint

T-bear

  • Full Member
  • ***
  • Posts: 160
Re: TStringList
« Reply #4 on: April 18, 2011, 11:02:34 am »
The stringlist is created, but that code doesnt seem to work. i get the message
unit2.pas(53,33) Error: Incompatible types: got "TStrings" expected "TStringList"
in the message window.

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3824
  • I like bugs.
Re: TStringList
« Reply #5 on: April 18, 2011, 11:20:12 am »
try this:

Code: Pascal  [Select][+][-]
  1. var
  2.   stringlist: TStringList;
  3. begin
  4.   stringlist := TStringList.Create;
  5.   stringlist := memo1.Lines;

Uhhh... whatever you tried to do, that is a memory leak. Please remember this is not Java with garbage collection.

The idea was to copy lines from stringlist to Memo, right. Try this:
Code: Pascal  [Select][+][-]
  1. memo1.Lines.Assign(stringlist);

I can recommend searching for code samples in Lazarus code base itself. Just use Ctrl-Shift-F, select Lazarus src top dir and search...

Juha
Mostly Lazarus trunk on Manjaro Linux.

vvzh

  • Jr. Member
  • **
  • Posts: 58
Re: TStringList
« Reply #6 on: April 18, 2011, 12:20:18 pm »
And when i want to do the opposite thing:
Stringlist.text:=Memo1.Lines
If you need a new, completely independent string list that holds a copy of Memo.Lines then just exchange two variables in the code Juha suggested:
Code: [Select]
Stringlist:=TStringList.Create;
Stringlist.Assign(Memo1.Lines);
// some operations with Stringlist
FreeAndNil(stringlist);

T-bear

  • Full Member
  • ***
  • Posts: 160
Re: TStringList
« Reply #7 on: April 18, 2011, 02:42:10 pm »
And how can i add the strings from one stringList to the end of another?

Thanks! :D

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3824
  • I like bugs.
Re: TStringList
« Reply #8 on: April 18, 2011, 02:55:30 pm »
And how can i add the strings from one stringList to the end of another?

How to Add Strings... use AddStrings()

You can guess many such functions by using the IDE's nice code browsing, code completion and hint features.
Type stringList and a dot (.), a list opens. Press 'a', select a func. Inside the parentesis do Ctrl-Shift-Space and you get a parameter list.
Ctrl-Click the function to go to its definition, press Ctrl-Up (or Ctrl-Down) for its implementation. The IDE is like a extension of your brain when used properly.

Juha
Mostly Lazarus trunk on Manjaro Linux.

 

TinyPortal © 2005-2018