Author: Jonas Bilinkevicius
Object List(String) Using TList
Answer:
Couldn't find too many examples on the net of how to do this so here it is.
Here some code for all you newbies(like myself kinda). That will let you create
your own objectlist.
I used code from a program that manages email accounts for this example..
add items via accountlist.add(TAccount.Create(Server, User, Password);
1 uses classes;
2 3 type4 5 //Define the type of data for it to hold6 TAccount = class7 private8 fServer: string;
9 fUser: string;
10 fPassword: string;
11 public12 constructor create(Server, User, Password: string);
13 property Server: stringread fServer write FServer;
14 property User: stringread fUser write FUser;
15 property Password: stringread fpassword write fpassword;
16 end;
17 // define the list18 TAccountList = class(TList)
19 private20 function GetItem(AIndex: Integer): TAccount;
21 public22 constructor create;
23 destructor Destroy; override;
24 function add(Account: TAccount): integer;
25 property Items[AIndex: Integer]: TAccount read getitem;
26 end;
27 implementation28 29 constructor TAccount.create(Server, User, Password: string);
30 begin31 fserver := Server;
32 fUser := User;
33 fPassword := Password;
34 end;
35 36 constructor TAccountlist.create;
37 begin38 inherited Create;
39 end;
40 41 destructor TAccountList.Destroy;
42 begin43 try44 Clear;
45 finally46 inherited Destroy;
47 end;
48 end;
49 50 function TAccountlist.add(Account: TAccount): integer;
51 begin52 result := inherited Add(Account);
53 end;
54 55 function TAccountList.GetItem(AIndex: integer): TAccount;
56 begin57 result := TAccount(inherited Items[AIndex]);
58 end;