Recent

Author Topic: SOLVED - formulario := form - does not recognize procedure  (Read 1933 times)

TeoUrbana

  • New Member
  • *
  • Posts: 43
    • Distração For Fun
SOLVED - formulario := form - does not recognize procedure
« on: March 30, 2019, 01:58:35 am »
Hi everyone! I'm using Google Translator (I speak portuguese).
Let's go there for my doubt.

It's the following ... I have a search form, which can be called by any other application form. So for this, the search form needs to know who called you.
Within the search form I have a procedure that will receive the form that called it and pass this information to a global variable (within the search unit), so that it can be used by other procedures.

Here is the search form call:
Code: Pascal  [Select][+][-]
  1. procedure TfrmCadPaciente.spdBtnLocalizaPaciente1Click(Sender: TObject);
  2. begin
  3.    try
  4.      frmLocalizaPaciente := TfrmLocalizaPaciente.Create(frmCadPaciente);  // criação do form de cadastro
  5.      frmLocalizaPaciente.FormQueChamou(Self); // passando o formulário de cadastro como parametro para a pesquisa
  6.      frmLocalizaPaciente.ShowModal; // chamando o formulário de pesquisa
  7.    finally
  8.      FreeAndNil(frmLocalizaPaciente);
  9.    end;
  10. end;

Inside the search form, I have the following:
Code: Pascal  [Select][+][-]
  1.   .
  2.   .
  3.   .
  4. var
  5.    frmLocalizaPaciente: TfrmLocalizaPaciente;
  6.    ControlePaciente : TControlePaciente;
  7.    formulario : TForm;
  8.  
  9. implementation
  10.    .
  11.    .
  12.    .
  13. procedure TfrmLocalizaPaciente.FormQueChamou(form: TForm);
  14. begin
  15.    formulario := form; <== form.ExibeDadosBasicos OK  -  formulario.ExibeDadosBasicos ERRO
  16. end;
  17.   .
  18.   .
  19.   .
  20.  

As can be seen, form is a global variable, of type TForm, and receives the value of form that was passed as parameter.
If I write form.ExibeDadosBasicos, it works perfectly.
If I write formulario.ExibeDadosBasicos, it informs that it does not identify the ExibeDadosBasicos member. How is it possible???
formulario: = form
If it works with form, should not it work with formulario?
In this case does anyone have a solution?

« Last Edit: May 06, 2019, 12:18:43 am by TeoUrbana »

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: formulario := form - does not recognize procedure
« Reply #1 on: March 30, 2019, 02:37:41 am »
It doesn't work because formulario is declared as an standard TForm, which doesn't have any method called ExibeDadosBasicos.

You can either declare:
Code: [Select]
formulario: TfrmCadPaciente:or use a cast when using it, like in:
Code: [Select]
(formulario as TfrmCadPaciente).ExibeDadosBasicos
That is assuming ExibeDadosBasicos is a method of TfrmCadPaciente; if not, replace by the proper form class.

It's strange, though, that:
Code: [Select]
form.ExibeDadosBasicosworks. It shouldn't for the same reason. Most queer, indeed.
« Last Edit: March 30, 2019, 02:40:02 am by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

TeoUrbana

  • New Member
  • *
  • Posts: 43
    • Distração For Fun
Re: formulario := form - does not recognize procedure
« Reply #2 on: April 03, 2019, 01:28:58 am »
Is weird...
If you do "ShowMessage (formulario.ClassName);" the return is TFrmCadPaciente.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: formulario := form - does not recognize procedure
« Reply #3 on: April 03, 2019, 02:48:49 am »
That is because at execution-time you pass a form of that class.

Your problem, however, comes about because the compiler can't know that at compile-time: since the parameter is a TForm you may pass any TForm descendant and the compilar has no way to know what (extra) methods that may have.

It's the same than with standard event handlers: the compiler only knows that they get passed a Sender: TObject so you have to cast it to the correct class to work with it; for example:
Code: Pascal  [Select][+][-]
  1. procedure ButtonClick(Sender: TObject);
  2. begin
  3.   ShowMessage(Sender.ClassName); {Will show "TButton"}
  4.   if Sender is TButton then
  5.     with Sender as TButton do begin
  6.       {Do whatever with the button}
  7.     end;
  8. end;
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

TeoUrbana

  • New Member
  • *
  • Posts: 43
    • Distração For Fun
SOLVED - Re: formulario := form - does not recognize procedure
« Reply #4 on: May 06, 2019, 12:15:08 am »
Thanks for the "lucamar" for the help. I found out where I was wrong.

I simply forgot to add the unit in the uses.
That done the code (form the TFrmCadPaciente) .ExhibeDadosBasicos worked.

Thanks again for your help.

 

TinyPortal © 2005-2018