Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

Subtract One MessageSet from Another

See more IMAP Examples

Demonstrates the Chilkat MessageSet.Subtract method, which removes from this set every identifier whose numeric value is also present in another MessageSet. The only argument is the other set. This example starts with ids 1:10, subtracts 2,4,6, and prints what remains.

Background: A common IMAP pattern is "process everything except what I've already handled." Subtract makes that a single call: remove the already-processed ids from a working set. The comparison is numeric only and this object's HasUids is preserved, so for meaningful results both sets should hold the same id type from the same mailbox. Passing the same object clears the set; passing an empty set changes nothing.

Chilkat Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.MessageSet;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  msgSet: TMessageSet;
  processed: TMessageSet;
  remaining: string;

begin
  success := False;

  //  Demonstrates the MessageSet.Subtract method, which removes from this set every identifier
  //  that is also present in another MessageSet.  The only argument is the other MessageSet.

  msgSet := TMessageSet.Create;

  //  Start with messages 1 through 10.
  success := msgSet.FromCompactString('1:10');
  if (success = False) then
    begin
      WriteLn(msgSet.LastErrorText);
      Exit;
    end;

  //  The identifiers that have already been processed and should be excluded.
  processed := TMessageSet.Create;
  success := processed.FromCompactString('2,4,6');
  if (success = False) then
    begin
      WriteLn(processed.LastErrorText);
      Exit;
    end;

  //  Remove the already-processed identifiers from msgSet.
  msgSet.Subtract(processed);

  //  Show what remains.
  remaining := msgSet.ToCompactString();
  WriteLn(remaining);
  //  Output: 1,3,5,7:10


  msgSet.Free;
  processed.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.