Recent

Author Topic: I have a "best practices" question with regard to text in a TMemo component.  (Read 1550 times)

Yukiko

  • Jr. Member
  • **
  • Posts: 55
I have written a configuration utility for a game emulator. It has a few TMemo components that display information to the user. My question is this: What is the best practices, or the "proper" way to store the text that will populate the TMemo.Lines?

As far as I know the options I have at my disposal are:
1. Store the text in a file and ReadLn each line and .Append the line to the TMemo.
2. Store the text lines in an array of strings and .Append each line to the TMemo.
3  Place the text in TMemo at design time.

I currently use two methods. The overall instructions are using the file method because I am still adding features and updating the instructions is easier that way but I have a couple forms where I filled the TMemo at design time.

This is not a burning issue but I wanted some input from more experienced programmers than myself.

Thaddy

  • Hero Member
  • *****
  • Posts: 14157
  • Probably until I exterminate Putin.
None of your given options seem correct to me, although any of them will work.
Tmemo.lines is of type TStrings so you can load and store from stream and/or file using Tstrings standard methods.
Any Tstrings is assignment compatible to another Tstrings and any Tstrings is backed by streams, so here are some better options:
Code: Pascal  [Select][+][-]
  1.   memo1.Lines.LoadFromFile(aFilename);
  2.   memo1.Lines.SaveToFile(aFilename);
  3.   memo1.Lines.Assign(aStringlist);
  4.   aStringlist.Assign(memo1.Lines);
  5.   memo1.lines.LoadFromStream(aStream); // any stream: e.g. file, memory, resource, network, database, etc
  6.   memo1.lines.SaveToStream(aStream);
  7.   memo1.lines.addstrings(aStringlist);
  8.   aStringlist.AddStrings(memo1.lines);
  9.   memo1.lines.Append(sString);
  10.   memo1.lines.AddText()
  11.   // etc
These methods are real "best practice".
See https://www.freepascal.org/docs-html/rtl/classes/tstrings.html

If the text is static, option 3 is the best you came up with yourself.
This will in fact use internally one of the options I showed you by use of component streaming.
If the text is dynamic use one of the Tstrings options.
« Last Edit: October 21, 2018, 08:14:19 am by Thaddy »
Specialize a type, not a var.

Yukiko

  • Jr. Member
  • **
  • Posts: 55
Thank you. Obviously you can tell I am still ignorant of a lot of what is possible with Lazarus/Free Pascal. So I appreciate the help and instruction.

Is it just that easy to do a
Code: Pascal  [Select][+][-]
  1. memo1.Lines.LoadFromFile(aFilename);,
assuming aFilename is a text file or do I need to do some prerequisite setup first? If it is that easy then I definitely feel like I am terribly lacking in my knowledge. :(

Thaddy

  • Hero Member
  • *****
  • Posts: 14157
  • Probably until I exterminate Putin.
Yes, it is that easy  :D
Specialize a type, not a var.

Windsurfer

  • Sr. Member
  • ****
  • Posts: 368
    • Windsurfer
It is not only newcomers who learn on this forum.

Yukiko

  • Jr. Member
  • **
  • Posts: 55
I thank you very much Thaddy. You have helped me simplify my code and answered my best practices question all in one post. This project of mine was started about 10 years ago and I dropped it for some reason 9 years ago and forgot I had started it until last year when I found the old files. So even though I am not new to Lazarus I am still a babe when it comes to many of the things that I should have learned.

You are correct Windsurfer. There is always something new to learn.

Oh and I appreciate that I received a direct answer rather than a "go read the docs" type of response that one might receive on other support sites.

 

TinyPortal © 2005-2018