Reputation: 14499
I'm looking for a better way to initialize my lst_devices : TArray<String>
variable.
Today, I'm doing it like this (which is very ugly and it's a code smell, but it works fine at least).
lst_devices_id := ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''];
How can I improve it?
Upvotes: 3
Views: 2048
Reputation: 613302
Strings are default initialized to the empty string. So you could rely on that like so:
SetLength(lst_devices_id, N);
Here you allocate the desired number of elements for the array and rely on the compiler to default initialize them to the empty string. Note that this relies on the array being previously uninitialized. If that it not the case then you would have to finalize it first.
Finalize(lst_devices_id);
SetLength(lst_devices_id, N);
Upvotes: 8