2007. november 27., kedd

Object List(String) Using TList


Problem/Question/Abstract:

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);

uses classes;

type

  //Define the type of data for it to hold
  TAccount = class
  private
    fServer: string;
    fUser: string;
    fPassword: string;
  public
    constructor create(Server, User, Password: string);
    property Server: string read fServer write FServer;
    property User: string read fUser write FUser;
    property Password: string read fpassword write fpassword;
  end;
  // define the list
  TAccountList = class(TList)
  private
    function GetItem(AIndex: Integer): TAccount;
  public
    constructor create;
    destructor Destroy; override;
    function add(Account: TAccount): integer;
    property Items[AIndex: Integer]: TAccount read getitem;
  end;
implementation

constructor TAccount.create(Server, User, Password: string);
begin
  fserver := Server;
  fUser := User;
  fPassword := Password;
end;

constructor TAccountlist.create;
begin
  inherited Create;
end;

destructor TAccountList.Destroy;
begin
  try
    Clear;
  finally
    inherited Destroy;
  end;
end;

function TAccountlist.add(Account: TAccount): integer;
begin
  result := inherited Add(Account);
end;

function TAccountList.GetItem(AIndex: integer): TAccount;
begin
  result := TAccount(inherited Items[AIndex]);
end;

Nincsenek megjegyzések:

Megjegyzés küldése