[fpc-pascal] What's wrong with this simple test program

Sven Barth pascaldragon at googlemail.com
Sat Dec 20 02:16:45 CET 2014


Am 19.12.2014 21:58 schrieb "Ralf Quint" <freedos.la at gmail.com>:
>
> On 12/19/2014 12:53 PM, Joost van der Sluis wrote:
>>
>> On 12/19/2014 09:48 PM, Ralf Quint wrote:
>>>
>>> I have been trying to use tStringList in a larger project of mine, but
>>> this just keeps bombing out with a exception.
>>> I have been able to reproduce the problem with this very simple test
>>> program:
>>>
>>> program project1;
>>>
>>> USES Classes;
>>>
>>> Var T : tStringList;
>>>      S : String;
>>> begin
>>>    S := 'Test';
>>>    T.Create;
>>> // T.Clear;
>>>    T.Add (S);
>>>    T.Free;
>>> end.
>>
>>
>> You're mixing old TP-style objects with classes.
>>
>> T := TStringList.Create is the correct syntax.
>
> Well, then why is the compiler building this just fine without even
throwing an error?!?!?

Because Create is a legal method call as well. You can call a constructor
like a normal method on an existing instance.

> As mentioned, I do not get the error at the .Create call, but at anything
after that...
> I do not have a working Delphi system at hand right now, but I am sure
that this would have compiled and worked on anything up to Delphi 2006 just
fine.

Delphi will compile this (like FPC does), but it will fail at runtime.
Trust me here, I did the mistake a few times myself when I thought about
too many other things.

Regards,
Sven
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20141220/fe3ad912/attachment.html>


More information about the fpc-pascal mailing list