He hecho el cambio de TList por TInterfaceList, el fallo ocurre al pulsar en boton3 y a la 1ª pasada. Lo q hace el programa es crear una Lista con Interfaces de un objeto q almacena 2 números (uno por cada interfaz). Pulsando el primer botón, crearía un objeto y lo guardaría como interfaz en miLista que es de tipo TInterfaceList (se tendrían q pulsar varias veces para crear varios números). Luego pulsando el segundo botón, tendría q aparecer en cada listBox los nºs almacenados, un nº por cada tipo. Pero al pulsarlos fallan los métodos para meter los valores de la interfaz en el TListBox.
Venga aqui os dejo el código, un saludo y gracias por la ayuda.
Código Delphi
[-]
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
INotas = interface
['{F5F6273D-A9C9-44C5-A666-A4F607E18374}']
function getValor: Integer;
end;
INotas2 = interface
['{ADDD2B20-BC87-11D9-9718-0050FCAA723B}']
function getValor2: Integer;
end;
TMisNotas = class(TInterfacedObject, INotas, INotas2)
private
numnotas, numnotas2: Integer;
public
constructor Create;
destructor Destroy;
function getValor: Integer;
function getValor2: Integer;
end;
TForm1 = class(TForm)
Boton: TButton;
Lista: TListBox;
Boton2: TButton;
Lista2: TListBox;
procedure BotonClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Boton2Click(Sender: TObject);
private
MiLista: TInterfaceList; procedure meterObjetoEnLista(interfaz: INotas);
procedure meterObjetoEnLista2(interfaz: INotas2);
public
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
constructor TMisNotas.Create;
begin
inherited Create;
numnotas:= Random(100);
numnotas2:= 100 + Random(100);
end;
destructor TMisNotas.Destroy;
begin
inherited Destroy;
end;
function TMisNotas.getValor: Integer;
begin
Result:= numnotas;
end;
function TMisNotas.getValor2: Integer;
begin
Result:= numnotas2;
end;
procedure TForm1.BotonClick(Sender: TObject);
var
Objeto: TMisNotas;
begin
MiLista.Add(TMisNotas.Create);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Randomize;
MiLista:= TInterfaceList.Create;
end;
procedure TForm1.meterObjetoEnLista(interfaz: INotas);
begin
Lista.Items.Add(IntToStr(interfaz.getValor));
end;
procedure TForm1.meterObjetoEnLista2(interfaz: INotas2);
begin
Lista2.Items.Add(IntToStr(interfaz.getValor2));
end;
procedure TForm1.Boton2Click(Sender: TObject);
var
i, cont: Integer;
begin
Lista.Clear;
Lista2.Clear;
cont:= MiLista.Count;
if (cont > 0) then
for i:= 0 to cont - 1 do
begin
meterObjetoEnLista( INotas( MiLista.Items[i] ) );
meterObjetoEnLista2( INotas2( MiLista.Items[i] ) );
end;
end;
end.