Recent

Author Topic: [SOLVED] Copy the contents of tStrings to another Tstrings.  (Read 586 times)

CM630

  • Hero Member
  • *****
  • Posts: 1169
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
[SOLVED] Copy the contents of tStrings to another Tstrings.
« on: September 16, 2024, 09:44:16 am »
I am trying to copy the contents of a ComboBox to a TStrings.

OldVals := ComboBox1.Items; does not copy them, but just creates a reference to the original one, so when ComboBox1.Items; is changed, OldVals is changed, too, which is not okay for the current purpose.

I also tried, the code below, but it results in RunError(211)... Call to abstract method.



Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   OldVals: TStrings;
  4. begin
  5.   OldVals := TStrings.Create;
  6.   OldVals.Assign(ComboBox1.Items);
  7. end;  

There is no example here: https://www.freepascal.org/docs-html/rtl/classes/tstrings.assign.html
So I wonder what is the straight forward way.

« Last Edit: September 16, 2024, 10:00:50 am by CM630 »
Лазар 3,4 32 bit (sometimes 64 bit); FPC3,2,2

AlexTP

  • Hero Member
  • *****
  • Posts: 2465
    • UVviewsoft
Re: Copy the contents of tStrings to another Tstrings.
« Reply #1 on: September 16, 2024, 09:46:30 am »
Instead of
Code: Pascal  [Select][+][-]
  1. OldVals := TStrings.Create;
Try
Code: Pascal  [Select][+][-]
  1. OldVals := TStringList.Create;

rvk

  • Hero Member
  • *****
  • Posts: 6388
Re: Copy the contents of tStrings to another Tstrings.
« Reply #2 on: September 16, 2024, 09:54:09 am »
As extra information from the manual:
Quote
TStrings implements an abstract class to manage an array of strings. It introduces methods to set and retrieve strings in the array, searching for a particular string, concatenating the strings and so on. It also allows an arbitrary object to be associated with each string.

It also introduces methods to manage a series of name=value settings, as found in many configuration files.

An instance of TStrings is never created directly, instead a descendant class such as TStringList should be created. This is because TStrings is an abstract class which does not implement all methods; TStrings also doesn't store any strings, this is the functionality introduced in descendants such as TStringList.

TStrings implements the IFPObserved interface: when the stringlist is changed, a ooChanged notification is sent to all observers.
https://freepascal.org/docs-html/rtl/classes/tstrings.html

So you should never create a TStrings directly byut always use a decendant of TStrings.

BTW. The variable itself (OldVals) is just a pointer so you could leave it as a TStrings because it can still hold a pointer to TStringList (but it doesn't hurt to make also make it a TStringList here just for clarity).

In TComboBox Items is declared as TStrings so it can also hold direct descendants from TStrings and doesn't necessarily need a descendant from TStringList.

CM630

  • Hero Member
  • *****
  • Posts: 1169
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: [SOLVED] Copy the contents of tStrings to another Tstrings.
« Reply #3 on: September 16, 2024, 10:01:05 am »
Thanks for the clarification, it works now!
Лазар 3,4 32 bit (sometimes 64 bit); FPC3,2,2

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1408
    • Lebeau Software
Re: Copy the contents of tStrings to another Tstrings.
« Reply #4 on: September 16, 2024, 05:50:41 pm »
So you should never create a TStrings directly byut always use a decendant of TStrings.

It has always annoyed me that Delphi and FreePascal allow abstract classes to be instantiated.  I have yet to encounter a scenario where that is a useful feature.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

simone

  • Hero Member
  • *****
  • Posts: 605
Re: [SOLVED] Copy the contents of tStrings to another Tstrings.
« Reply #5 on: September 16, 2024, 09:01:34 pm »
It has always annoyed me that Delphi and FreePascal allow abstract classes to be instantiated.  I have yet to encounter a scenario where that is a useful feature.

Absolutely agree.

As stated in:

https://www.freepascal.org/docs-html/3.2.0/ref/refse35.html

this behavior is due to compatibility with Delphi, but I think it may be misleading, as in this case.

However, the compiler emits a warning message.
Microsoft Windows 10 64 bit - Lazarus 3.0 FPC 3.2.2 x86_64-win64-win32/win64

Thaddy

  • Hero Member
  • *****
  • Posts: 15717
  • Censorship about opinions does not belong here.
Re: [SOLVED] Copy the contents of tStrings to another Tstrings.
« Reply #6 on: September 16, 2024, 09:05:37 pm »
It is not always true:
Code: Pascal  [Select][+][-]
  1. program testabstract;
  2. {$ifdef fpc}{$mode delphi}{$warn CONSTRUCTING_ABSTRACT error}{$endif}
  3. type
  4.    TAbstractClass = class abstract
  5.    public
  6.      procedure testme;virtual;abstract;
  7.    end;
  8.  
  9. var
  10.   Test:TAbstractClass;
  11. begin
  12.   Test := TAbstractClass.Create;
  13. end.
FreePascal can prevent you from compiling that. Half documented.
With the above provisions the code will not compile.
If I smell bad code it usually is bad code and that includes my own code.

simone

  • Hero Member
  • *****
  • Posts: 605
Re: [SOLVED] Copy the contents of tStrings to another Tstrings.
« Reply #7 on: September 16, 2024, 09:19:33 pm »
@Thaddy: You're right. I forgot. It's documented here:

https://www.freepascal.org/docs-html/prog/progsu80.html
Microsoft Windows 10 64 bit - Lazarus 3.0 FPC 3.2.2 x86_64-win64-win32/win64

Zoran

  • Hero Member
  • *****
  • Posts: 1853
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: [SOLVED] Copy the contents of tStrings to another Tstrings.
« Reply #8 on: September 16, 2024, 09:26:38 pm »
It is not always true:
Code: Pascal  [Select][+][-]
  1. program testabstract;
  2. {$ifdef fpc}{$mode delphi}{$warn CONSTRUCTING_ABSTRACT error}{$endif}
  3. type
  4.    TAbstractClass = class abstract
  5.    public
  6.      procedure testme;virtual;abstract;
  7.    end;
  8.  
  9. var
  10.   Test:TAbstractClass;
  11. begin
  12.   Test := TAbstractClass.Create;
  13. end.
FreePascal can prevent you from compiling that. Half documented.
With the above provisions the code will not compile.

Thank you for this, Thaddy!

PascalDragon

  • Hero Member
  • *****
  • Posts: 5678
  • Compiler Developer
Re: Copy the contents of tStrings to another Tstrings.
« Reply #9 on: September 19, 2024, 09:48:10 pm »
So you should never create a TStrings directly byut always use a decendant of TStrings.

And there should have been at least a warning from the compiler (the same warning that can be escalated to error using {$WARN CONSTRUCTING_ABSTRACT ERROR} as mentioned by Thaddy).

 

TinyPortal © 2005-2018