From: Neven MacEwan [mailto:[EMAIL PROTECTED]]
>I'd hate to mention how a ForEach Method would be useful (an save my OOS)
>
>ASet.ForEach(Method)

Hey, just encapsulate each set in a class!  I mean, you don't have
Byte.ForEachBit(AMethod) either, do you?! ;-)

You can have a foreach procedure of sorts for sets, as follows

type
  TByteMe = procedure (AValue: Byte) of object;

procedure ForEach(const ASet; AMethod: TByteMe);
var LSet: set of Byte absolute ASet;
  i: Byte;
begin
  for i := 0 to 255 do
    if i in LSet then
      AMethod(i);
end;

procedure TForm1.Button1Click(Sender: TObject);
var MySet: set of Char;
begin
  MySet := ['a', 'b', 'x'];
  ForEach(MySet, ForEachChar);
end;

procedure TForm1.ForEachChar(AValue: Byte);
var LChar: Char absolute AValue;
begin
  ShowMessage(LChar);
end;

Note that I didn't claim it was efficient! :-)  Here is an improvement (?)
for sets of less than 256 elements:

procedure ForEach(const ASet; ALow, AHigh: Byte; AMethod: TByteMe);
var LSet: set of Byte absolute ASet;
  i: Byte;
begin
  for i := ALow to AHigh do
    if i in LSet then
      AMethod(i);
end;

procedure TForm1.Button1Click(Sender: TObject);
var MySet: set of Char;
begin
  MySet := ['a', 'b', 'x'];
  ForEach(MySet, Byte(Low(Char)), Byte(High(Char)), ForEachChar);
end;

NB., Don't try 

ForEach([1, 2], ...);  // Oops ;-)

Cheers,
Carl
---------------------------------------------------------------------------
    New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
                  Website: http://www.delphi.org.nz
To UnSub, send email to: [EMAIL PROTECTED] 
with body of "unsubscribe delphi"

Reply via email to