[fpc-pascal]Why TStrings.Add can't work?
memsom at interalpha.co.uk
memsom at interalpha.co.uk
Fri Nov 1 17:15:04 CET 2002
> Hello, everybody
>
> I wonder why the following codes will cause a runtime error of "Abstract
> method called"?
What Peter said ;-) A method marked with the identifier 'abstract' has no
implementation. It is a 'pure virtual' method in C++ speak. Only a subclass
that 'overrides' the method and provides an implementation can have the method
validly called.
> And TStringList is also weird, though run properly, the result is wrong:
This is your mistake. It stems from a lesser known ability of the TStringList
to hold values in the format 'Name=Value', like in an Inifile. If you had
values in that format, you could call 'sl.Names[i];' and get the 'name' section
of that item.
e.g.
sl.add('one=1'); //index 0
sl.add('onetwo=2'); //index 1
sl.add('onethree=3'); //index 2
sl.Names[1] = 'two' (zero based index)
sl.Values['two'] = 2
Therefore:
sl.Values[sl.Names[1]] = 2
The solution to your problem is to use either of the following:
s := sl[i];
(or)
s := sl.strings[i];
They are both exactly the same. Delphi has the concept of default parameters,
and 'strings' is the default for the TStringList (FPC people please feel free
to correct me if the default concept is not imcluded in FPC.)
sl.Names[i] is simply blank because the string is in the wrong format.
Incedentally, you can say:
writeln(sl[i]);
if you like. You don't have to explicitly create your own storage for it.
Matt
---------------------------------------------
This message was sent using Mistral WebMail.
http://www.mistral.co.uk/
More information about the fpc-pascal
mailing list