Delphi : Handling the fact that Strings are not Objects

Posted by awmross on Stack Overflow See other posts from Stack Overflow or by awmross
Published on 2010-05-26T02:22:52Z Indexed on 2010/05/26 2:41 UTC
Read the original article Hit count: 298

Filed under:
|

I am trying to write a function that takes any TList and returns a String representation of all the elements of the TList.

I tried a function like so

function ListToString(list:TList<TObject>):String;

This works fine, except you can't pass a TList to it.

E2010 Incompatible types: 'TList<System.TObject>' and 'TList<System.string>'

In Delphi, a String is not an Object. To solve this, I've written a second function:

function StringListToString(list:TList<string>):String;

Is this the only solution? Are there other ways to treat a String as more 'object-like'?

In a similar vein, I also wanted to write an 'equals' function to compare two TLists. Again I run into the same problem

function AreListsEqual(list1:TList<TObject>; list2:TList<TObject>):boolean;

Is there any way to write this function (perhaps using generics?) so it can also handle a TList? Are there any other tricks or 'best practises' I should know about when trying to create code that handles both Strings and Objects? Or do I just create two versions of every function? Can generics help?

I am from a Java background but now work in Delphi. It seems they are lately adding a lot of things to Delphi from the Java world (or perhaps the C# world, which copied them from Java). Like adding equals() and hashcode() to TObject, and creating a generic Collections framework etc. I'm wondering if these additions are very practical if you can't use Strings with them.

© Stack Overflow or respective owner

Related posts about delphi

Related posts about delphi-2010