How to initialize a TList<T> in one step using Delphi?
Asked Answered
E

2

8

I am sure this is a easy question, but I cannot get it to run:

var
  FMyList: TList<String>;
begin
  FMyList := TList<String>.Create(?????);
end;

How to insert instead of ????? to insert this 3 strings:

'one'
'two'
'three'

Thanks..

Erg answered 24/4, 2011 at 11:40 Comment(2)
Create the list and then insert with FMyList.Add.Madaih
Yes, I am doing now in this way, but I would like to do that in one step.Erg
Q
6

There is no single method to do this. You could write your own constructor to do this as so:

constructor TMyList<T>.Create(const Values: array of T);
var
  Value: T;
begin
  inherited Create;
  for Value in Values do
    Add(Value);
end;

Then you could write:

FList := TMyList<string>.Create(['one', 'two', 'three']);

Update

As Uwe correctly points out in his answer, the code I present should use the AddRange() method:

constructor TMyList<T>.Create(const Values: array of T);
begin
  inherited Create;
  AddRange(Values);
end;
Quintessa answered 24/4, 2011 at 12:21 Comment(3)
I was asking for a languaje-already-existant way of do it... But this is a good approximation in absence of this functionality...Erg
@FerPt I know that's what you were asking for, hence my first sentence.Quintessa
I have tried to get your approximation with Class Helpers... No luck.. Seems no way to use class helpers on generics... :-( Seems your answer is the only valid in Delphi XE.Erg
D
17

Not a one liner, but a two liner:

FMyList := TList<String>.Create;
FMyList.AddRange(['one', 'two', 'three']);

Edit: Of course you can combine it with David's approach.

Dyche answered 24/4, 2011 at 15:53 Comment(4)
+1 I'm not familiar with AddRange, thanks for pointing it out.Quintessa
Thanks Uwe, I think I cannot distribute the 'accepted answer' to David and you. Both are good answers. Thanks again.Erg
I think two lines is better than one line here, and I deplore Constructor side effects.Tyrant
And one line of variable declaration. And if it is the only variable : one line of var keyword. So something between 3 to 4 lines.Sarene
Q
6

There is no single method to do this. You could write your own constructor to do this as so:

constructor TMyList<T>.Create(const Values: array of T);
var
  Value: T;
begin
  inherited Create;
  for Value in Values do
    Add(Value);
end;

Then you could write:

FList := TMyList<string>.Create(['one', 'two', 'three']);

Update

As Uwe correctly points out in his answer, the code I present should use the AddRange() method:

constructor TMyList<T>.Create(const Values: array of T);
begin
  inherited Create;
  AddRange(Values);
end;
Quintessa answered 24/4, 2011 at 12:21 Comment(3)
I was asking for a languaje-already-existant way of do it... But this is a good approximation in absence of this functionality...Erg
@FerPt I know that's what you were asking for, hence my first sentence.Quintessa
I have tried to get your approximation with Class Helpers... No luck.. Seems no way to use class helpers on generics... :-( Seems your answer is the only valid in Delphi XE.Erg

© 2022 - 2024 — McMap. All rights reserved.