Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   OOP (https://www.clubdelphi.com/foros/forumdisplay.php?f=5)
-   -   Dudas sobre interfaces... (https://www.clubdelphi.com/foros/showthread.php?t=24937)

OscarG 07-09-2005 10:32:30

Dudas sobre interfaces...
 
Hola, tengo unas dudas sobre el uso de interfaces....

Por ahora paso un programilla y me gustaría saber xq falla y luego me gustaría hacer más preguntas...

El programa tiene 2 botones y 2 listas...
pulsando varias veces al primer botón se crearían varios objetos q luego
al pulsar en el segundo botón, los mostraría en las listas usando las interfaces...
Hay 2 interfaces y cada uno se muestra en una de las listas (TListBox).

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
     { Private declarations }
     Inota: INotas;
     MiLista: TList;
     procedure meterObjetoEnLista(interfaz: INotas);
     procedure meterObjetoEnLista2(interfaz: INotas2);
   public
     { Public declarations }
   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:= TList.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( TMisNotas( MiLista.Items[i] ) ) );
       meterObjetoEnLista2( INotas2( TMisNotas( MiLista.Items[i] ) ) );
     end;
 end;
 
 end.

roman 07-09-2005 16:08:59

Pues hombre, sería recomendable que indicaras qué problemas te está dando.

A ojo de buen o mal cubero te puedo decir que estás rompiendo la regla de oro para el uso de interfaces en Delphi: nunca mezclar referencias a interfaz con referencias a objetos y mucho menos hacer moldeos del tipo

Código Delphi [-]
Lista.Items.Add(IntToStr(interfaz.getValor));

Aquí interfaz es una referencia a la interfaz INotas lo cual aumenta el número de referencias a ella. Pero al hacer el moldeo y agregarlo como si fuese un entero a Lista, no hay manera de decrementar la referencia en algún momento y el objeto que implementa la interfaz se queda colgado en memoria.

Tendría que revisar con más detalle pero el punto es que el mezclar objetos e interfaces generalmente provoca una desincronización en el número de referencias a la interfaz lo que usualmente termina en un "Invalid pointer operation" o un "Access Violation".

Para empezar, si requieres guardar listas de interfaces entonces usa algo más apropiado como TInterfaceList.

// Saludos

mamcx 07-09-2005 16:10:45

Aja...

y la duda es???

OscarG 09-09-2005 14:20:50

Más datos...
 
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
    { Private declarations }
    MiLista:  TInterfaceList; //TList;
    procedure meterObjetoEnLista(interfaz:  INotas);
    procedure meterObjetoEnLista2(interfaz: INotas2);
  public
    { Public declarations }
  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.


La franja horaria es GMT +2. Ahora son las 01:49:26.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi