Club Delphi  
    FTP   CCD     Buscar   Trucos   Trabajo   Foros

Retroceder   Foros Club Delphi > Principal > OOP
Registrarse FAQ Miembros Calendario Guía de estilo Temas de Hoy

Grupo de Teaming del ClubDelphi

Respuesta
 
Herramientas Buscar en Tema Desplegado
  #1  
Antiguo 21-09-2005
OscarG OscarG is offline
Miembro
 
Registrado: sep 2005
Posts: 35
Poder: 0
OscarG Va por buen camino
Problemas con los interfaces...

Hola!
Estoy intentando comprender las interfaces, mas o menos lo tengo controlado, pero cuando me complico un poco me empiezan a salir errores raros (para mi).

Bueno, he simplificado mi problema a una pequeña aplicación de interfaces y me gustaría saber xq falla, la idea del programa es que tiene un objeto 2 interfaces. Por otra parte tengo 2 TListBox y 2 botones, el de arriba va creado objetos y metiendolos en una lista. y el de abajo, después de haber pulsado varias veces al de arriba, mostraría el contenido de la lista de objetos creado en los TListBox, cada objeto como tiene 2 interfaces, cada uno se usa para guardar un nº, con lo q luego aparecería en los TListBox divididos según el interfaz q sea. En esta versión cambie de un TList a TInterfaceList gracias al consejo del Moderador Roman. Pero no se solucionó del todo el problema y sigue fallando.
Me gustaría saber xq falla y así por una parte conocer mejor las interfaces y por otra poder aplicarlo a mis aplicaciones.

Gracias por las molestias y un saludo.
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.

Última edición por Neftali [Germán.Estévez] fecha: 21-09-2005 a las 13:02:51. Razón: Añadir TAG's al código
Responder Con Cita
  #2  
Antiguo 21-09-2005
Avatar de Neftali [Germán.Estévez]
Neftali [Germán.Estévez] Neftali [Germán.Estévez] is offline
[becario]
 
Registrado: jul 2004
Ubicación: Barcelona - España
Posts: 18.297
Poder: 10
Neftali [Germán.Estévez] Es un diamante en brutoNeftali [Germán.Estévez] Es un diamante en brutoNeftali [Germán.Estévez] Es un diamante en bruto
Por favor, utiliza los TAG's de: delphi(entre corchetes) y /delphi (entre corchetes) cuando coloques código.
En éste caso ya te los he modificado yo.

http://www.clubdelphi.com/foros/misc.php?do=bbcode
__________________
Germán Estévez => Web/Blog
Guía de estilo, Guía alternativa
Utiliza TAG's en tus mensajes.
Contactar con el Clubdelphi

P.D: Más tiempo dedicado a la pregunta=Mejores respuestas.
Responder Con Cita
  #3  
Antiguo 21-09-2005
rounin rounin is offline
Miembro
 
Registrado: sep 2005
Posts: 43
Poder: 0
rounin Va por buen camino
Interfaces can be cast to other one using as operator (or QueryInterface):
MiLista.Items[i] as INotas,
MiLista.Items[i] as INotas2

(When you cast an object to any class, the result as a pointer will be the same.
But it is wrong for interfaces, and "INotas(MiLista.Items[i])" is not a correct cast)
Responder Con Cita
  #4  
Antiguo 22-09-2005
OscarG OscarG is offline
Miembro
 
Registrado: sep 2005
Posts: 35
Poder: 0
OscarG Va por buen camino
Funciona!! OK!

Thanks for your help!
Responder Con Cita
Respuesta



Normas de Publicación
no Puedes crear nuevos temas
no Puedes responder a temas
no Puedes adjuntar archivos
no Puedes editar tus mensajes

El código vB está habilitado
Las caritas están habilitado
Código [IMG] está habilitado
Código HTML está deshabilitado
Saltar a Foro


La franja horaria es GMT +2. Ahora son las 20:25:06.


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