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?
Mailing lists / fpc-pascal / 2011-02
Pre-initialising a TStringList
6 messages · 4 people ·
- JJuha Manninen 2
- MMark Morgan Lloyd 2
- HHoward Page-Clark 1
- SSven Barth 1
Reply by email Subscribe to this list Your subscription
Read-only archive. Use j and k to move between messages.
Mark Morgan Lloyd wroteGiven 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
Howard Page-Clark wrote 21 lines
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
Sven Barth kirjoitti sunnuntai 06 helmikuu 2011 16:24:49:
Show quoted text 8 lines
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
Juha Manninen wrote 24 lines
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 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
