home *** CD-ROM | disk | FTP | other *** search
- Program StrList;
- (*Copyright (c) 1992 KHIRON Software
-
- All rights reserved. KHIRON Software hereby grants
- permission for free distribution of this software,
- and for use of this software within commercial and
- non-commercial applications. This software itself
- may not be distributed commercially without obtaining
- written permission from KHIRON Software.
-
- Should you use this software in commercial product send
- me a postcard at: KHIRON Software
- P.O. Box 544
- INDOOROOPILLY Qld 4068
- AUSTRALIA
- *)
- { This is a simple exaple of creating your own functional Collection
- Object. It derives a descendant of collection to store string
- pointers that overrides the FreeItem to Dispose of a String
- Pointer when the objects is disposed, and adds a new method called
- Print_Sentence which prints out all the Strings in order.
- }
- Uses Objects;
- Type
- PMyStrings = ^TMyStrings;
- TMyStrings = Object(tCollection)
- Constructor Init;
- Procedure Print_Sentence;
- procedure FreeItem(Item: Pointer); virtual;
- end;
- Constructor TMyStrings.Init;
- begin
- TCollection.Init(10,10);
- end;
- procedure TMyStrings.FreeItem(Item: Pointer);
- begin
- DisposeStr(PString(Item));
- end;
- Procedure TMyStrings.Print_Sentence;
- Procedure PrintWord(Item : PString); far;
- begin
- If Item <> nil then
- Write(Item^,' ');
- end;
- begin
- ForEach(@PrintWord);
- Writeln('.');
- end;
- Var
- MyStringList : pMyStrings;
- Temp : String;
- begin
- MyStringList := New(pMyStrings,Init);
- MyStringList^.Insert(NewStr('Four'));
- MyStringList^.Insert(NewStr('Score'));
- MyStringList^.Insert(NewStr('and'));
- MyStringList^.Insert(NewStr('Twenty'));
- MyStringList^.Insert(NewStr('Beers'));
- MyStringList^.Insert(NewStr('Ago'));
- MyStringList^.Print_Sentence;
- Temp := PString(MyStringList^.At(3))^;
- Writeln('The Fourth Word is ',Temp);
- Dispose(MyStringList,Done);
- end.
-
-