Recent

Author Topic: TStrings [SOLVED]  (Read 4288 times)

pascal111

  • Sr. Member
  • ****
  • Posts: 423
  • Un trabajo en equipo para programas serias.
TStrings [SOLVED]
« on: May 29, 2021, 07:23:23 pm »
لم أكن أعلم أنّ في لغات البرمجة المرئيّة أدوات للبيانات غير الأدوات الكلاسيكيّة مثل المصفوفات والمُتغيّرات والمؤشرات ،وكُنتُ أظنّ أنّ الـ data structure أو بنى البيانات المُتقدمّة عبارةٌ عن تلكـ النماذج المذكورة في مرجع الـ Turbo Pascal ولغة الـ الـ C++ أشياءٌ مثل الـ trees والـ lists ولكنّني في هذا الرابط وجدتُّ أنواعاً أخرى https://wiki.freepascal.org/Data_Structures,_Containers,_Collections وأُريدُ أن أستفسر عن كيفيّة الإستفادة من الـ TStrings فرُبّما العمل مع الحرفيّات ليس مُعقداً مثل الحسابات الرقميّة.

كيف يُمكنني الإستفادة من الـ TStrings في برامجي؟

google translate:

"I did not know that in visual programming languages ​​there are tools for data other than classic tools such as arrays, variables and indicators "pointers", and I used to think that the data structure or advanced data structures are those models mentioned in the Turbo Pascal reference and C ++ language things like trees and lists, but in This link found other types https://wiki.freepascal.org/Data_Structures,_Containers,_Collections I want to inquire about how to take advantage of TStrings, maybe working with craftsmen "strings" is not as complicated as digital calculations.

How can I use TStrings for my programs?"
« Last Edit: May 29, 2021, 09:43:27 pm by pascal111 »
La chose par la chose est rappelé.

jamie

  • Hero Member
  • *****
  • Posts: 6130
Re: TStrings
« Reply #1 on: May 29, 2021, 07:41:34 pm »
Tstrings is a base class.. it has lots of Abstract members in it that needs to be filled in if you decide to use them, otherwise you do not need to fill them in..

 There are methods that are not abstract but do depend on certain abstract members to be implemented.

 Normally Tstrings is used in calling parameters to allow any class to be accepted that is based from Tstrings or you can create a class of your own using TSTRINGs as the inherited class and thus you implement your version of the abstract members.

 Doing this, all functions that knows how to interact with a class that is based from a TString will function correctly...

 you can view the source code of the TStrings to get a better idea..

 I wanted to add that also there are many virtual methods that really do nothing but can be overridden..
« Last Edit: May 29, 2021, 07:45:00 pm by jamie »
The only true wisdom is knowing you know nothing

MarkMLl

  • Hero Member
  • *****
  • Posts: 6686
Re: TStrings
« Reply #2 on: May 29, 2021, 07:58:05 pm »
Extensive documentation available via https://www.freepascal.org/docs.html , in particular https://www.freepascal.org/docs-html/current/rtl/classes/index.html noting that these are part of FPC not Lazarus/LCL. And in this case there's plenty of useful material in the Wiki... just remember that the documentation has precedence.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Handoko

  • Hero Member
  • *****
  • Posts: 5153
  • My goal: build my own game engine using Lazarus
Re: TStrings
« Reply #3 on: May 29, 2021, 08:09:18 pm »
Don't use TStrings directly, instead use TStringList. TStringList is a class inherited from TStrings. You only use TStrings if you want to write your own class based on it.

I believe you already know what array is. You can think TStringList as a kind of array for storing string data.
https://www.freepascal.org/docs-html/rtl/classes/tstringlist.html

Here is a very simple demo showing how to use TStringList:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Dialogs, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     Button2: TButton;
  17.     procedure Button1Click(Sender: TObject);
  18.     procedure Button2Click(Sender: TObject);
  19.     procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
  20.     procedure FormCreate(Sender: TObject);
  21.   private
  22.     FVisitorNames: TStringList;
  23.   end;
  24.  
  25. var
  26.   Form1: TForm1;
  27.  
  28. implementation
  29.  
  30. {$R *.lfm}
  31.  
  32. { TForm1 }
  33.  
  34. procedure TForm1.FormCreate(Sender: TObject);
  35. begin
  36.   FVisitorNames   := TStringList.Create;
  37.   Button1.Caption := 'Add a visitor';
  38.   Button2.Caption := 'No more visitor';
  39.   Button2.Visible := False;
  40. end;
  41.  
  42. procedure TForm1.Button1Click(Sender: TObject);
  43. var
  44.   S: string;
  45. begin
  46.  
  47.   // Ask
  48.   S := InputBox('Add Visitor', 'Name', '');
  49.   if S = '' then Exit;
  50.   FVisitorNames.Add(S);
  51.  
  52.   // Ask more
  53.   Button1.Caption := 'Add more visitor';
  54.  
  55.   // At least 3 visitors
  56.   if FVisitorNames.Count >= 3 then
  57.     Button2.Visible := True;
  58.  
  59. end;
  60.  
  61. procedure TForm1.Button2Click(Sender: TObject);
  62. var
  63.   S: string;
  64. begin
  65.   S := FVisitorNames[0] + ' is the first visitor.' + LineEnding +
  66.        FVisitorNames[FVisitorNames.Count-1] + ' is the last visitor.' + LineEnding +
  67.        'The total visitors are ' + FVisitorNames.Count.ToString + ' persons.';
  68.   ShowMessage(S);
  69. end;
  70.  
  71. procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
  72. begin
  73.   FVisitorNames.Free;
  74. end;
  75.  
  76. end.

pascal111

  • Sr. Member
  • ****
  • Posts: 423
  • Un trabajo en equipo para programas serias.
Re: TStrings
« Reply #4 on: May 29, 2021, 09:26:46 pm »
Don't use TStrings directly, instead use TStringList. TStringList is a class inherited from TStrings. You only use TStrings if you want to write your own class based on it.

I believe you already know what array is. You can think TStringList as a kind of array for storing string data.
https://www.freepascal.org/docs-html/rtl/classes/tstringlist.html

Here is a very simple demo showing how to use TStringList:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Dialogs, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     Button2: TButton;
  17.     procedure Button1Click(Sender: TObject);
  18.     procedure Button2Click(Sender: TObject);
  19.     procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
  20.     procedure FormCreate(Sender: TObject);
  21.   private
  22.     FVisitorNames: TStringList;
  23.   end;
  24.  
  25. var
  26.   Form1: TForm1;
  27.  
  28. implementation
  29.  
  30. {$R *.lfm}
  31.  
  32. { TForm1 }
  33.  
  34. procedure TForm1.FormCreate(Sender: TObject);
  35. begin
  36.   FVisitorNames   := TStringList.Create;
  37.   Button1.Caption := 'Add a visitor';
  38.   Button2.Caption := 'No more visitor';
  39.   Button2.Visible := False;
  40. end;
  41.  
  42. procedure TForm1.Button1Click(Sender: TObject);
  43. var
  44.   S: string;
  45. begin
  46.  
  47.   // Ask
  48.   S := InputBox('Add Visitor', 'Name', '');
  49.   if S = '' then Exit;
  50.   FVisitorNames.Add(S);
  51.  
  52.   // Ask more
  53.   Button1.Caption := 'Add more visitor';
  54.  
  55.   // At least 3 visitors
  56.   if FVisitorNames.Count >= 3 then
  57.     Button2.Visible := True;
  58.  
  59. end;
  60.  
  61. procedure TForm1.Button2Click(Sender: TObject);
  62. var
  63.   S: string;
  64. begin
  65.   S := FVisitorNames[0] + ' is the first visitor.' + LineEnding +
  66.        FVisitorNames[FVisitorNames.Count-1] + ' is the last visitor.' + LineEnding +
  67.        'The total visitors are ' + FVisitorNames.Count.ToString + ' persons.';
  68.   ShowMessage(S);
  69. end;
  70.  
  71. procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
  72. begin
  73.   FVisitorNames.Free;
  74. end;
  75.  
  76. end.

كلامكـ صائب ،فيبدو أنّ هناكـ تصنيفاً للأصناف داخل وحدات الـ Free Pascal ولكل صنف وظائف وتخصص.

google translate:

"Your words are correct, it seems that there is a classification of items "classes" within the Free Pascal units, and each class has functions and specializations."
La chose par la chose est rappelé.

Handoko

  • Hero Member
  • *****
  • Posts: 5153
  • My goal: build my own game engine using Lazarus
Re: TStrings
« Reply #5 on: May 29, 2021, 09:30:58 pm »
For your information, abstract class basically means you should not use it directly but you write your own class based on it. TStrings is an abstract class.

https://freepascal.org/docs-html/ref/refse36.html#x71-950006.2
« Last Edit: May 29, 2021, 09:34:05 pm by Handoko »

pascal111

  • Sr. Member
  • ****
  • Posts: 423
  • Un trabajo en equipo para programas serias.
Re: TStrings
« Reply #6 on: May 29, 2021, 10:03:36 pm »
Tstrings is a base class.. it has lots of Abstract members in it that needs to be filled in if you decide to use them, otherwise you do not need to fill them in..

 There are methods that are not abstract but do depend on certain abstract members to be implemented.

 Normally Tstrings is used in calling parameters to allow any class to be accepted that is based from Tstrings or you can create a class of your own using TSTRINGs as the inherited class and thus you implement your version of the abstract members.

 Doing this, all functions that knows how to interact with a class that is based from a TString will function correctly...

 you can view the source code of the TStrings to get a better idea..

 I wanted to add that also there are many virtual methods that really do nothing but can be overridden..

بما أنّ لديكـ فكرةً جيّدةً عن الـ interfaces فهل يُمكنكـ أن تُخبرني عن الـ interfaces المُسمّى IFPObserved والذي يدخل في بناء الصنف القاعدي لـ TStrings والمُسمّى TPersistent في هذا الرابط https://lazarus-ccr.sourceforge.io/docs/rtl/classes/tpersistent.html وهذا الرابط https://lazarus-ccr.sourceforge.io/docs/rtl/classes/ifpobserved.html ،يبدو أنّ لديكـ فكرةً جيّدة تُحاول سبر أسرار الـ TStrings؟

google translate:

"Since you have a good idea of ​​interfaces, can you tell me about the interfaces called IFPObserved that are included in the construction of the base class for TStrings called TPersistent in this link https://lazarus-ccr.sourceforge.io/docs/rtl/classes /tpersistent.html and this link https://lazarus-ccr.sourceforge.io/docs/rtl/classes/ifpobserved.html Do you seem to have a good idea trying to fathom the secrets of TStrings?"
La chose par la chose est rappelé.

jamie

  • Hero Member
  • *****
  • Posts: 6130
Re: TStrings [SOLVED]
« Reply #7 on: May 30, 2021, 12:36:50 am »
look here.
https://www.freepascal.org/docs-html/rtl/classes/ifpobserver.html

That is the interface that receives notification messages from the observed objects

to use that, you can insert this in your class definition..

 TFORM1 = Class(Tform, IfpObserver);

You will get a compiler error of course because you need to implement the method also..
Code: Pascal  [Select][+][-]
  1. Procedure FpObserveredChange(ASender:TObject; Operation:TObserveredOoperation;Data:Pointer);
  2.  

Put that in the Tform1 Class and implement the body of it like you would any other method..

So this is the concept behind it. You create an object that has a IfpObverser interface, for example a TstringList and you can add a observer to the list. In this case if you were to do as I showed you above the TFORM1 would be the observer.

  When ever the stringlist makes a change it sends notices to all the observers in the list. the Observers receive this and can do as they please with it.

I can make an example if you wish..

The only true wisdom is knowing you know nothing

jamie

  • Hero Member
  • *****
  • Posts: 6130
Re: TStrings [SOLVED]
« Reply #8 on: May 30, 2021, 01:05:59 am »
An Example project

The only true wisdom is knowing you know nothing

MarkMLl

  • Hero Member
  • *****
  • Posts: 6686
Re: TStrings
« Reply #9 on: May 30, 2021, 08:30:01 am »
google translate:

"Since you have a good idea of ​​interfaces, can you tell me about the interfaces called IFPObserved that are included in the construction of the base class for TStrings called TPersistent in this link https://lazarus-ccr.sourceforge.io/docs/rtl/classes /tpersistent.html and this link https://lazarus-ccr.sourceforge.io/docs/rtl/classes/ifpobserved.html Do you seem to have a good idea trying to fathom the secrets of TStrings?"

I find it a little disturbing that a user should be investigating interfaces and deeply-defined abstract classes before getting to grips with classes like TStringList which have immediate practical use. @Pascal111, what's going on here? Are you trying to teach yourself Pascal by reading the reference manual in page order, in the same way that Gabriel Kron taught himself English?

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

pascal111

  • Sr. Member
  • ****
  • Posts: 423
  • Un trabajo en equipo para programas serias.
Re: TStrings
« Reply #10 on: May 30, 2021, 02:15:04 pm »
google translate:

"Since you have a good idea of ​​interfaces, can you tell me about the interfaces called IFPObserved that are included in the construction of the base class for TStrings called TPersistent in this link https://lazarus-ccr.sourceforge.io/docs/rtl/classes /tpersistent.html and this link https://lazarus-ccr.sourceforge.io/docs/rtl/classes/ifpobserved.html Do you seem to have a good idea trying to fathom the secrets of TStrings?"

I find it a little disturbing that a user should be investigating interfaces and deeply-defined abstract classes before getting to grips with classes like TStringList which have immediate practical use. @Pascal111, what's going on here? Are you trying to teach yourself Pascal by reading the reference manual in page order, in the same way that Gabriel Kron taught himself English?

MarkMLl

سوف أتعلّم TStringList بالطبع ولكن لا مانع من أخذ فكرة عن أصناف أخرى وأنواعها وبعض المعلومات عنها وإلا لن تكون هناكـ ثقافة حول Free Pascal.

بمناسبة ذِكر Gabriel Kron فلقد وجدت هذا الجزء الذي يتكلّم عن تعلّمه الإنجليزيّة:

google translate:

"I will be learning TStringList of course, but I don't mind getting an idea of ​​other brands "classes" and their types and some information about them, otherwise there will be no culture about Free Pascal.

On the occasion of Gabriel Kron's mention, I found this section on his learning of English:"

Quote
It rather disappointed me to realize that familiarity with German and French would not be of much practical use in my attempts to relieve Europe of my presence. I sensed - in spite of the arguments of those who had travelled far and wide - that once Europe receded behind my back the only language that would enable me to move about freely must be English. Unfortunately (or fortunately) no English-Hungarian dictionary could be procured in all Transylvania, and the only one I could get hold of was a big Muret-Sanders type of English-German dictionary, containing over a hundred thousand words. Of course it was out of the question to repeat the stunt of tearing out the pages and memorizing them one after another, as they contained so many archaic and technical words. What I did was to borrow an English book, which happened to be H.G. Wells' The Food of the Gods, and beginning with the first page I wrote out a hundred words each day and committed them to memory. After eight or ten pages the text began to assume more human form, and past the first chapter the content even became enjoyable.

http://www.quantum-chemistry-history.com/Kron_Dat/KronGabriel1.htm

لسوء الحظ ،لم تعُد طريقته في تعلّم الإنجليزيّة صالحةً لي الآن ،إلا أنّها طريقة رائعة وذكيّة وعاملة.

google translate:

"Unfortunately, his method of learning English no longer works for me now, but it is wonderful, smart and working (way)."
La chose par la chose est rappelé.

pascal111

  • Sr. Member
  • ****
  • Posts: 423
  • Un trabajo en equipo para programas serias.
Re: TStrings [SOLVED]
« Reply #11 on: May 30, 2021, 08:23:56 pm »
إليكُم هذا البرنامج البسيط والذي يستخدم الـ TStringlist.

google translate:

"Here is a simple program that uses the TStringlist."

(https://i.postimg.cc/jDLMfmC1/Screenshot-at-2021-05-30-20-12-47.png)

https://www.mediafire.com/file/5f6jnuqmccccel3/Project_13.rar/file
La chose par la chose est rappelé.

 

TinyPortal © 2005-2018