Forum > General

Creating Array Of Objects

<< < (2/2)

Thaddy:
It looks like basically a song collection.
Here is how to handle that with the classes unit. I will add an example with the objects unit later, in the case that you prefer objects over classes.
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---{$mode objfpc}{$apptype console}uses  classes;type   { TSong }   TSong = class(TCollectionItem)  private    FName:string;    FArtist:string;    FAlbum:string;    FLength: integer;  public    constructor Create(ACollection: TCollection); override;  published    //handled through TCollectionItem.ID    //property ID:Integer read FID write FID;    property Name:string read Fname write FName;    property Artist:string read FArtist write FArtist;    property Album:string  read FAlbum write FAlbum;    property Length: integer read FLength write FLength;  end;   { TSongCollection }   TSongCollection = class(TCollection)  private    function GetItems(Index: integer): TSong;    procedure SetItems(Index: integer; AValue: TSong);  public    constructor Create;  public    function Add: TSong;    property Items[Index: integer]: TSong read GetItems write SetItems; default;  end;  constructor TSong.Create(ACollection: TCollection);begin  if Assigned(ACollection) then    inherited Create(ACollection);end; { TSongCollection } function TSongCollection.GetItems(Index: integer): TSong;begin  Result := TSong(inherited Items[Index]);end; procedure TSongCollection.SetItems(Index: integer; AValue: TSong);begin  Items[Index].Assign(AValue);end; constructor TSongCollection.Create;begin  inherited Create(TSong);end; function TSongCollection.Add: TSong;begin  Result := inherited Add as TSong;end; var  Songs: TSongCollection;  NewSong:TSong;  i:integer;begin  Songs := TSongCollection.Create;  NewSong := Songs.Add;  NewSong.Name := 'Jumping Jack Flash';  NewSong.Artist:= 'The Rolling Stones';  NewSong.Album := 'The Rolling Stones Rock and Roll Circus';  NewSong.Length := 100;  for TCollectionItem(NewSong) in Songs do    begin     writeln(Newsong.name);     writeln(NewSong.ID);    end;  Songs.Free;  readln;end.

Thaddy:
And here is one that uses the objects unit instead:
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---{$mode objfpc}{$apptype console}uses  objects;type   { TSong }  PSong = ^TSong;  TSong = object  private    FName,    FArtist,    FAlbum:string;  public    constructor Init(aName,aArtist,aAlbum:string);    destructor done; virtual;    property Name  :string read Fname write FName;    property Artist:string read FArtist write FArtist;    property Album :string read FAlbum write FAlbum;  end;    { TSongCollection }  PSongCollection = ^TSongCollection;  TSongCollection = object(TCollection)  private    function GetItems(Index: integer): TSong;virtual;    procedure SetItems(Index: integer; AValue: TSong);virtual;  public    destructor Done;virtual;    property Items[Index: integer]: TSong read GetItems write SetItems; default;  end; constructor TSong.Init(aName, aArtist, aAlbum: string);begin  Name := aName;  Artist:= aArtist;  Album := aAlbum;end; destructor Tsong.Done;begin  //inherited done;end; { TSongCollection } function TSongCollection.GetItems(Index: integer): TSong;begin  Result := TSong(At(Index)^);end; procedure TSongCollection.SetItems(Index: integer; AValue: TSong);begin   inherited Insert(PSong(@AValue));end; destructor TSongCollection.Done;begin   DeleteAll;   Inherited Done;end;  var  Songs: PSongCollection;  Song : PSong;  i:integer;begin  { Songs collection with initial capacity of 50 and grow factor 10 }  New(Songs, init(50,10));  New(Song,Init('Jumping Jack Flash',                   'The Rolling Stones',                   'The Rolling Stones Rock and Roll Circus'));  Songs^.Insert(Song);  New(Song,Init('It''s Only Rock ''n Roll (But I Like It)',                   'The Rolling Stones',                   'It''s Only Rock ''n Roll'));  Songs^.Insert(Song);  writeln(songs^.count);  writeln(Songs^.Items[0].name);  writeln(Songs^.Items[0].Artist);  writeln(Songs^.Items[0].Album);  writeln;  writeln(Songs^.Items[1].name);  writeln(Songs^.Items[1].Artist);  writeln(Songs^.Items[1].Album);  Dispose(Song, done);  Dispose(Songs,done);  readln;end.Example inspired by the TCollection example in the Turbo Vision 2.0 programming guide, 1992.
This is available from the internet archive, but I also have it in print.

Better not mix the objects.TCollection with classes.TCollection etc: They are very very different.

nikel:
Thanks for the replies. I'm working on Thaddy's first example.

Thaddy:
@nikel
I will show you how to load/save the collection for both examples tomorrow.

Navigation

[0] Message Index

[*] Previous page

Go to full version