[fpc-devel] TStrings Add(array of string)

Sven Barth pascaldragon at googlemail.com
Sat Mar 31 21:39:38 CEST 2012


Am 31.03.2012 20:27 schrieb "Andrew Haines" <AndrewD207 at aol.com>:
>
> I was thinking about TStrings possibly having an overloaded function Add
> or AddStrings where the argument is an array of string?
>
> procedure Add(strs: array of string); overload;
> or
> procedure AddStrings(strs: array of string); overload;
>
> Specifically I want this for TProcess.Parameters. since it's a bit of a
> pain to type
> Proc.Parameters.Add('-Arg1');
> Proc.Parameters.Add('Arg1_Value');
> Proc.Parameters.Add('-Arg2');
> Proc.Parameters.Add('Arg2_Value');
> Proc.Parameters.Add('-Arg3');
> Proc.Parameters.Add('Arg3_Value');
>
> even "with Proc.Parameters do begin Add(n); ... end; " is a bit ugly
> when parameters have arguments.
>
> when I could type:
>
> Proc.Parameters.Add(['-Arg1', 'Arg1_Value']);
> Proc.Parameters.Add(['-Arg2', 'Arg2_Value']);
> Proc.Parameters.Add(['-Arg3', 'Arg3_Value']);
>
> or even
>
> Proc.Parameters.Add(['-Arg1','Arg1_Value','-Arg2','Arg2_Value','-Arg3',
> 'Arg3_Value']);
>
> A less generic alternative would be adding a procedure to TProcess
> AddArgs(args: array of string);
>
> Is this a good idea?
>
> If yes then I can create a patch.

Independently of whether a patch will be accepted or not, you can do the
following beginning with 2.6.0:

=== source begin ===

type
  TStringsHelper = class helper for TStrings
   procedure AddStrings(const aStr: array of String); overload; // this is
needed if we don't want to hide the original AddStrings method
  end;

procedure TStringsHelper.AddStrings(const aStr: array of String);
var
  s: String;
begin
  for s in aStr do
    Add(s);
end;

// anywhere you want to use AddStrings with a TStrings instance or one of
its descendants you need to put the unit of TStringsHelper into the
uses-clause and then do
YourTStringsVar.AddStrings(['Hello', 'World']);

=== source end ===

You can do something similar for TProcess.

Regards,
Sven
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20120331/749f15e8/attachment.html>


More information about the fpc-devel mailing list