Doubt referring to circular Referral of type customer <-> room

Asked

Viewed 191 times

3

Well, I had asked but I guess it wasn’t clear so I’ll edit as you suggested, basically I wanted to do an application in Delphi using a Tsala class and several Tpessoa classes, however, the Tpessoa class needs to refer to Tsala in order to take information from her and modify it when necessary, and the classroom class needs a reference to the Tpessoa class in order to create a list of people in the room and be able to modify these people.

For example: Do something like this somewhere in the Tpessoa class:

self.sala.pegarPessoaPorNome("Alberto").Dar("Refrigerante");

Already in the Tsala class would have something like this:

function pegarPessoaPorNome(nome: string): TPessoa;
begin
 for pessoa in self.pessoas do
  if pessoa.nome = nome then Exit(pessoa);
end;

However, here comes the problem, since these two classes are in separate Units, it would give an error in the "circular reference" compiler because I am using the Tsala class in the Tpessoa class and the Tpessoa class in the Tsala class.. How could I fix this without having to appeal to Opointer or Tobject?

  • I don’t know if I understand clearly what you need. But your problem can be solved with Objectlist<T>. What happens is that with Objectlist your Tpessoa class will belong to Tsala, that is, Cvoce will have a Listasala.ListaPessoa. If you agree with the idea, I can put together a prototype of what it would look like.

  • Victor Tadashi, I didn’t understand was anything in this question, if that’s what you understood until you go, but if that’s not it there, I have to study more!

  • Daniel Yohan, explains why you need it there, at least an explanation of the use, if not your question will be closed, if it is not possible to replicate the error/problem, so this does not exist. Explain the need, maybe we can help you some other way!

1 answer

2


The correct thing, in this case, is to create the Tsala class as a Tpessoa Collection (or, the room class has a field that points to that Collection)

You can paste a lot of code if you already do your classes inheriting from the already defined types Tcollection and Tcollectionitem.

interface uses System.Classes;

TYPE
TPessoa=Class;{note aqui uma pré-declaração. Permite usar TPessoa em TSala antes de implementa-la}
TSala=Class(TCollection)
public function add:TPessoa;
function pegarPorNome(Nome:String):TPessoa;
property Pessoa(Const Nome:String):TPessoa read pegarPorNome;
constructor create(ItemClass:TCollectionItemClass);
end;

TPessoa=Class(TCollectionItem)
  Nome:String;
  //demais variaveis
  procedure DarQualquerCoisa();
  procedure FazerQualquerCoisa();
end;

implementation

{ TSala }

function TSala.add: TPessoa;
begin
Result := inherited Add as TPessoa;
end;

constructor TSala.create(ItemClass: TCollectionItemClass);
begin
 inherited Create(ItemClass);
end;

function TSala.pegarPorNome(Nome: String): TPessoa;
begin
result := nil;  //se nao encontrar o nome, retorna vazio
for I := 0 to Count-1 do
   begin
     if TPessoa(Items[i]).Nome=Nome then
     begin
     result := Items[i] as TPessoa;
     break;
     end;
   end;
if result = nil then
  showmessage(format('Nome "%s" não encontrado.',Nome));//Nao encontrou esse nome, voce pode emitir um aviso.
end;

{ TPessoa }

procedure TPessoa.DarQualquerCoisa();
begin
;
end;
procedure TPessoa.FazerQualquerCoisa();
begin
;
end;

Finally, how to use this class

procedure TForm1.FormCreate(Sender: TObject);
begin
  Sala1 := TSala.Create;//Sala1 deve ser uma variavel publica
end; 

You can create people in two ways

procedure TForm1.BotaoAddPessoa1Click(Sender: TObject);
var Nova_Pessoa:TPessoa;
begin
 Nova_Pessoa:=Sala.Add;
 Nova_Pessoa.Nome := Edit1.Text;
 //... etc
 Nova_Pessoa.FazerQualquerCoisa();
 Nova_Pessoa.DarQualquerCoisa();
end; 

procedure TForm1.BotaoAddPessoa2Click(Sender: TObject);
begin
//esse é o jeito mais interessante,pois nao precisa declarar variavel
 With Sala.Add do begin
   Nome := Edit1.Text;
//... etc
 FazerQualquerCoisa();
 DarQualquerCoisa();
 end;

end; 
  • 1

    Thank you very much!

  • I forgot to wipe from memory, example using With With Sala.Add from Begin Name := Edit1.Text; //... etc Do something(); Daranythingthing(); Free;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.