Forum > General

Creating Array Of Objects

(1/2) > >>

nikel:
Hello, I'm trying to create array of objects. I have TMain, TEntireSongs and TSong classes. Here's my code


--- 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";}};} ---unit EntireSongsHandler; {$mode ObjFPC}{$H+} interface uses  Classes, SysUtils,  Config, Token; type  TEntireSongs = object  private    List      : TStringList;    FArrSongs : array[0..200, 1..12] of string;    Token     : PToken;  public    constructor Init;    destructor Done;     function GetSongByIndex(Num: Integer): TSong;    ...
... and TSong class


--- 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";}};} ---unit songhandler; {$mode ObjFPC}{$H+}{$TYPEDADDRESS ON} interface uses  Classes, SysUtils,  Token, Config, HistoryHandler; type  TSong = object(TEntireSongs)  private    FID        : Integer;    FEnabled   : Boolean;    FPlayCount : Integer;    FAlbum     : string;    FArtist    : string;    FTitle     : string;    FDuration  : Integer;    FBpm       : Integer;    FKey       : shortstring;    FDate      : shortstring;    FPriority  : Byte;    FPath      : string;  public    property ID          : Integer read FID write FID;    ...
I'm getting error

--- Quote ---entiresongshandler.pas(21,50) Error: Identifier not found "TSong"
--- End quote ---

I tried TEntireSongs = array[0..200, 1..12] of string; but didn't work.

How can I fix errors?

WooBean:

--- Quote from: nikel on May 21, 2024, 03:37:44 pm ---Hello, I'm trying to create array of objects. I have TMain, TEntireSongs and TSong classes. Here's my code


--- 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";}};} ---unit EntireSongsHandler; {$mode ObjFPC}{$H+} interface uses  Classes, SysUtils,  Config, Token, songhandler; //<- here addtype  TEntireSongs = object  private    List      : TStringList;    FArrSongs : array[0..200, 1..12] of string;    Token     : PToken;  public    constructor Init;    destructor Done;     function GetSongByIndex(Num: Integer): TSong;    ...
...
How can I fix errors?

--- End quote ---

But this will lead to circular unit reference so put both units in one.

nikel:
Thanks for the quick reply. I tried using one file but got the same error. Here's my code so far


--- 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";}};} ---unit EntireSongsHandler; {$mode ObjFPC}{$H+} interface uses  Classes, SysUtils,  Config, Token; type  TEntireSongs = object  private    List      : TStringList;    FArrSongs : array[0..200, 1..12] of string;    Token     : PToken;  public    constructor Init;    destructor Done;     function GetSongByIndex(Num: Integer): TSong;    function GetSongByPath(P: string): TSong;     property ArrSongs : array[0..200, 1..12] of string read FArrSongs write FArrSongs;  end;   TSong = object(TEntireSongs)  private    FID        : Integer;    FEnabled   : Boolean;    FPlayCount : Integer;    FAlbum     : string;    FArtist    : string;    FTitle     : string;    FDuration  : Integer;    FBpm       : Integer;    FKey       : shortstring;    FDate      : shortstring;    FPriority  : Byte;    FPath      : string;  public    property ID          : Integer read FID write FID;    ...
Modify:


--- 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";}};} ---unit EntireSongsHandler; {$mode ObjFPC}{$H+} interface uses  Classes, SysUtils,  Config, Token, SongHandler; type  TEntireSongs = object  private    List      : TStringList;    FArrSongs : array[0..200, 1..12] of string;    Token     : PToken;  public    constructor Init;    destructor Done;     function GetSongByIndex(Num: Integer): TSong;    function GetSongByPath(P: string): TSong;     property ArrSongs : array[0..200, 1..12] of string read FArrSongs write FArrSongs;  end;   TSong = object(TEntireSongs)  private    FID        : Integer;    ...
This gives me error:


--- Quote ---entiresongshandler.pas(21,44) Error: Identifier not found "TSong"
--- End quote ---

WooBean:
If you want to have access to unit EntireSongsHandler from (still existing) unit songhandler you need to add them (EntireSongsHandler) in uses clause of songhandler unit.

It may be used in implementation part (all depends on how all application is designed).







Martin_fr:
You can't have circular references between "object". Unless you use pointers. Though maybe you meant to use classes, which can have such circles?

"object" in that aspect acts like "record"

You have

--- 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";}};} ---type  TEntireSongs = object  private...     function GetSongByIndex(Num: Integer): TSong; // FORWARD REFERENCE NOT POSSIBLE  end;  TSong = object(TEntireSongs)  // SINCE IT IS BASED ON TEntireSongs, IT CANNOT MOVE BEFORE TSong...  end; 

Is there a reason why you use "object" and not "class" ? (Mind "class" needs changes to the code, such as proper creation of instances).

Because with class you can do that. You can forward define "TSong = class" and then later define the details of TSong.


With object you can only forward define pointers to it


--- 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";}};} ---type  PSong = ^TSong;  // pointer to TSong, and in this case TSong can be defined later   TEntireSongs = object  private...     function GetSongByIndex(Num: Integer): PSong; // returns pointer  end;  TSong = object(TEntireSongs)...  end; 

Navigation

[0] Message Index

[#] Next page

Go to full version