Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2011-02

Pre-initialising a TStringList

6 messages · 4 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Mark Morgan Lloyd

Given one or more lines of text which are known at compilation time, and without the requirement to internationalize (these are, by RFC, US ASCII), what is the best way to get them into a TStringList?

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]

Reply by email

Howard Page-Clark
to Mark Morgan Lloyd
Mark Morgan Lloyd wrote
Given one or more lines of text which are known at compilation time, and without the requirement to internationalize (these are, by RFC, US ASCII), what is the best way to get them into a TStringList?

Perhaps there are better ways than the straightforward way?

const StringsToUse: string = 'Line 1'+LineEnding+
                              'Line 2'+LineEnding+
                              // + ...
                              'Line n';
var  FPreInitialisedSList: TStringList;
begin
   FPreInitialisedSList := TStringList.Create;
   FPreInitialisedSList.SetText(PChar(StringsToUse));
   // ...
   FPreInitialisedSList.Free;

H

Reply by email

Sven Barth
to Howard Page-Clark
Howard Page-Clark wrote 21 lines
On 06/2/11 1:55, Mark Morgan Lloyd wrote: > Given one or more lines of text which are known at compilation time, and > without the requirement to internationalize (these are, by RFC, US > ASCII), what is the best way to get them into a TStringList? Perhaps there are better ways than the straightforward way? const StringsToUse: string = 'Line 1'+LineEnding+ 'Line 2'+LineEnding+ // + ... 'Line n'; var FPreInitialisedSList: TStringList; begin FPreInitialisedSList := TStringList.Create; FPreInitialisedSList.SetText(PChar(StringsToUse)); // ... FPreInitialisedSList.Free;

Why are you using "PChar(StringsToUse)"? The following should be sufficient as well:

FPreInitialisedSList.Text := StringsToUse;

Note: You can also define StringsToUse as a typeless constant, e.g.:

const StringsToUse = 'Line 1' + LineEnding +
			'Line 2' + LineEnding +
			// + ...
			'Line n';

Regards,
Sven

Reply by email

Juha Manninen
to Sven Barth

Sven Barth kirjoitti sunnuntai 06 helmikuu 2011 16:24:49:

Show quoted text 8 lines
FPreInitialisedSList.Text := StringsToUse; Note: You can also define StringsToUse as a typeless constant, e.g.: const StringsToUse = 'Line 1' + LineEnding + 'Line 2' + LineEnding + // + ... 'Line n';

The most obvious solution is the best this time.

List.Add('Line 1');
List.Add('Line 2');
...
List.Add('Line n');

Setting the Text property (or calling the SetText method) parses the text and splits it by NewLine chars. It makes no sense because you have concatenated the strings with NewLines.
Method Add does not need to parse anything.

None of these solutions is "preinitialised", compile time solution. You can only initialize a const static arrays at compile time.

Juha

Reply by email

Mark Morgan Lloyd
to Juha Manninen
Juha Manninen wrote 24 lines
Sven Barth kirjoitti sunnuntai 06 helmikuu 2011 16:24:49: > FPreInitialisedSList.Text := StringsToUse; > > Note: You can also define StringsToUse as a typeless constant, e.g.: > > const StringsToUse = 'Line 1' + LineEnding + > 'Line 2' + LineEnding + > // + ... > 'Line n'; The most obvious solution is the best this time. List.Add('Line 1'); List.Add('Line 2'); ... List.Add('Line n'); Setting the Text property (or calling the SetText method) parses the text and splits it by NewLine chars. It makes no sense because you have concatenated the strings with NewLines. Method Add does not need to parse anything. None of these solutions is "preinitialised", compile time solution. You can only initialize a const static arrays at compile time.

Thanks for that Juha (et al.). I wanted to make sure that I wasn't missing something that I really should have known.

Is it possible to set up the text as an attribute (I'm avoiding the word property here for obvious reasons) of the entire class rather than of an instance?

Should I be looking at resources here and if so could they be kept in the main source file?

This is for a highly-experimental SNMP MIB to Pascal translator that I'm tinkering with so- as an example- there will be a single class describing an HP Ethernet switch which may be instantiated but will have common descriptive text.

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]

Reply by email

Juha Manninen
to Mark Morgan Lloyd

Mark Morgan Lloyd kirjoitti sunnuntai 06 helmikuu 2011 17:50:05:

Is it possible to set up the text as an attribute (I'm avoiding the word property here for obvious reasons) of the entire class rather than of an instance?

The const array could be outside of any class.
It is global if declared in interface section, or local to one unit if declared in implementation section.
Simple but it works well.

Juha

Reply by email