[fpc-pascal] Pre-initialising a TStringList

Sven Barth pascaldragon at googlemail.com
Sun Feb 6 15:24:49 CET 2011


On 06.02.2011 15:19, Howard Page-Clark wrote:
> 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



More information about the fpc-pascal mailing list