Sign in Join

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

Mailing lists / fpc-pascal / 2002-11

Why TStrings.Add can't work?

5 messages · 4 people ·

Reply by email Subscribe to this list Your subscription

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

mili

Hello, everybody

I wonder why the following codes will cause a runtime error of "Abstract method called"?

>>>>>
var
  slist:TStrings;
  i:integer;
begin
  slist:=TStrings.Create;
  slist.Add('hello!');
  slist.Free;
end.

<<<<<<

And TStringList is also weird, though run properly, the result is wrong:

>>>>>
var
  sl:TStringlist;
  i:integer;
  s:String;
begin
  sl:=TStringlist.Create;
  sl.Add('green');
  sl.Add('red');
  sl.Add('blue');
  for i:=0 to sl.Count-1 do begin
    s:=sl.Names[i];
    writeln(s);//Here output a blank line!!!! Why?
  end;
  sl.Free;
end.

<<<<<<

Thanks,
mili

Peter Vreman
to mili
you wrote
Hello, everybody I wonder why the following codes will cause a runtime error of "Abstract method called"?

TStrings is an abstract class. You need to use TStringList

Peter

Unknown author
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/

Unknown author
sl.add('one=1'); //index 0 sl.add('onetwo=2'); //index 1 sl.add('onethree=3'); //index 2

Hmmm... microsoft and cut/paste sucks!!

That should read:

sl.add('one=1'); //index 0 sl.add('two=2'); //index 1 sl.add('three=3'); //index 2

Hopefully that make more sense ;-)

Matt

---------------------------------------------
This message was sent using Mistral WebMail.
http://www.mistral.co.uk/