Note that there are some explanatory texts on larger screens.

plurals
  1. PODelphi : Handling the fact that Strings are not Objects
    text
    copied!<p>I am trying to write a function that takes any TList and returns a String representation of all the elements of the TList.</p> <p>I tried a function like so</p> <pre><code>function ListToString(list:TList&lt;TObject&gt;):String; </code></pre> <p>This works fine, except you can't pass a <code>TList&lt;String&gt;</code> to it.</p> <pre><code>E2010 Incompatible types: 'TList&lt;System.TObject&gt;' and 'TList&lt;System.string&gt;' </code></pre> <p>In Delphi, a String is not an Object. To solve this, I've written a second function:</p> <pre><code>function StringListToString(list:TList&lt;string&gt;):String; </code></pre> <p>Is this the only solution? Are there other ways to treat a String as more 'object-like'?</p> <p>In a similar vein, I also wanted to write an 'equals' function to compare two TLists. Again I run into the same problem</p> <pre><code>function AreListsEqual(list1:TList&lt;TObject&gt;; list2:TList&lt;TObject&gt;):boolean; </code></pre> <p>Is there any way to write this function (perhaps using generics?) so it can also handle a <code>TList&lt;String&gt;</code>? 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?</p> <p>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.</p> <p>[edit: Someone mentioned TStringList. I've used that up till now, but I'm asking about TList. I'm trying to work out if using TList for everything (including Strings) is a cleaner way to go.]</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload