5

in the previous ( remove empty strings from list ) question I asked about the removal of empty strings from a stringlist

....
// Clear out the items that are empty
for I := mylist.count - 1 downto 0 do
begin
  if Trim(mylist[I]) = '' then
    mylist.Delete(I);
end;
....

From the aspect of code design and reuse I would now prefer a solution being more flexible as :

 MyExtendedStringlist = Class(TStringlist)

 procedure RemoveEmptyStrings;

 end;

Q : Can I use a class helper in this case ? How would this look like in contrast to designing a new class as above ?

3
  • The most easy solution actually would be procedure RemoveEmptyStrings(s: TStrings);. Making that a class helper is just syntactic sugar. Apr 9, 2014 at 12:51
  • @stefan the entire thrust of the question is how to apply that sugar Apr 9, 2014 at 13:24
  • I know but if you look at the linked question (and its answer) at no point it was shown that you actually can solve this problem in a standalone routine which would be the most easy way. It does not require a subclass nor a helper to do this. And pointing this out was the intention of my previous comment. Apr 9, 2014 at 13:40

2 Answers 2

10

A class helper is an excellent idea here. To make it more widely applicable you should choose to associate the helper with the least derived class to which the helper can apply. In this case that means TStrings.

The huge advantage over deriving a new class is that your helper methods are available for instances of TStrings that are not created by you. Obvious examples include the TStrings properties that expose the contents of memos, list boxes etc.

I personally would write a helper that offers a more general removal functionality using a predicate. For instance:

type
  TStringsHelper = class helper for TStrings
  public
    procedure RemoveIf(const Predicate: TPredicate<string>);
    procedure RemoveEmptyStrings;
  end;

procedure TStringsHelper.RemoveIf(const Predicate: TPredicate<string>);
var
  Index: Integer;
begin
  for Index := Count-1 downto 0 do
    if Predicate(Self[Index]) then
      Delete(Index);
end;

procedure TStringsHelper.RemoveEmptyStrings;
begin
  RemoveIf(
    function(Item: string): Boolean
    begin
      Result := Item.IsEmpty;
    end;
  );
end;

More generally, TStrings is an excellent candidate for a class helper. It is missing quite a deal of useful functionality. My helper includes:

  • An AddFmt method that formats and adds in one go.
  • An AddStrings method that adds multiple items in one call.
  • A Contains method that wraps up IndexOf(...)<>-1 and presents a more semantically meaningful method to future readers of code.
  • A Data[] property, of type NativeInt, and matching AddData method, that wraps the Objects[] property. This hides the casts between TObject and NativeInt.

I'm sure there is more useful functionality that could be added.

1
  • class helper and Predicate: TPredicate are 2 new items for me; still learning ....
    – Franz
    Apr 9, 2014 at 11:13
7

You can use a HelperClass, but you should base on TStrings, which would offer more flexibility.

An Example could be:

type
TMyStringsClassHelper = class helper for TStrings
       Procedure RemoveEmptyItems;
    end;

{ TMyStringsClassHelper }

procedure TMyStringsClassHelper.RemoveEmptyItems;
var
 i:Integer;
begin
  for i := Count - 1 downto 0 do
    if Self[i]='' then Delete(i);

end;

procedure TForm5.Button1Click(Sender: TObject);
var
 sl:TStringList;
begin
  sl:=TStringList.Create;
  sl.Add('AAA');
  sl.Add('');
  sl.Add('BBB');
  sl.RemoveEmptyItems;
  Showmessage(sl.Text);
  Listbox1.Items.RemoveEmptyItems;
  Memo1.Lines.RemoveEmptyItems;
  sl.Free;
end;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.