Recent

Author Topic: Problem with calling procdeure from different Units  (Read 11382 times)

ciammaruca

  • New Member
  • *
  • Posts: 20
Re: Problem with calling procdeure from different Units
« Reply #15 on: October 22, 2014, 05:16:24 pm »
hi,
yes, but again: if i backward want to work from Unit2 in Unit1 Form1?
For example, how to create a TEdit in Form1 from Unit2? If i use this below in Unit2, identifer Form1 is not found obviusly!
So, how to make find Form1 from Unit2 ?
thanks

var
  g: TEdit;

begin
  g:=TEdit.Create(Form1);
  g.Parent:=Form1;
end;
« Last Edit: October 22, 2014, 05:18:28 pm by ciammaruca »

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: Problem with calling procdeure from different Units
« Reply #16 on: October 22, 2014, 05:38:36 pm »
You need to create the second uses section.
Code: [Select]
...

implementation

uses Unit1;

...
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Problem with calling procdeure from different Units
« Reply #17 on: October 22, 2014, 05:46:47 pm »
Here's an example of one way to accomplish what Blaazen explains.

Form1
Code: [Select]
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Forms, StdCtrls;

type

  TForm1 = class(TForm)
  public
    function CreateForm1Edit: TEdit;
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

function TForm1.CreateForm1Edit: TEdit;
begin
  Result:=TEdit.Create(Self);
  Result.Parent:=Self;
end;

end.

Form2
Code: [Select]
unit Unit2;

{$mode objfpc}{$H+}

interface

uses
  Forms, StdCtrls;

type

  TForm2 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    aUnit1Edit: TEdit;
  end;

var
  Form2: TForm2;

implementation

uses Unit1;

{$R *.lfm}

procedure TForm2.FormCreate(Sender: TObject);
begin
  aUnit1Edit:=Form1.CreateForm1Edit;
  aUnit1Edit.SetBounds(10,10,120,23);
  aUnit1Edit.Caption:='Created by Unit2';
end;

end.


ciammaruca

  • New Member
  • *
  • Posts: 20
Re: Problem with calling procdeure from different Units
« Reply #18 on: October 22, 2014, 09:41:06 pm »
gracias caballeros.
One more help, if possible, just to speeding my study: following my example, how to get events of my "on the fly/runtime" TEdit object? So for my example, how to get TForm1.eClick(Sender: TObject) from the both cases, from Unit1 and from Unit2?
muchas gracias a todo el mundo

cdbc

  • Hero Member
  • *****
  • Posts: 1083
    • http://www.cdbc.dk
Re: Problem with calling procdeure from different Units
« Reply #19 on: October 22, 2014, 10:28:18 pm »
Hi
Here's one: http://www.tutorialspoint.com/pascal/
Or... Search for "marco cantu pascal" on google  :D
Edit: for studying/learning pascal  8)
Regards Benny
« Last Edit: October 22, 2014, 10:32:14 pm by cdbc »
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

ciammaruca

  • New Member
  • *
  • Posts: 20
Re: Problem with calling procdeure from different Units
« Reply #20 on: October 23, 2014, 11:46:44 pm »
hi again,
no, i can't find the way to call events of runtime objects. So, please, as brief as i can:
- from Unit1 Form1 i call the function Test() in Unit2.
- Test() function in Unit2 creates a new Edit:
implementation
   uses Unit1;
procedure  editCambiato(Sender: TObject);
procedure test();
var
  e: TEdit;
begin
    e:=TEdit.Create(Form1);
    e.Parent:=Form1;
    e.Left:=10;
    e.Top:=50;
   e.OnClick:=@editCambiato; //<-----but this does not work!
end;

The "e.OnClick:=@editCambiato;" line makes this error:
unit2.pas(26,15) Error: Incompatible types: got "<address of procedure(TObject);Register>" expected "<procedure variable type of procedure(TObject) of object;Register>"
So, how to call the e.OnClick event in/from Unit2?

thank you

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Problem with calling procdeure from different Units
« Reply #21 on: October 24, 2014, 04:05:14 am »
unit2.pas(26,15) Error: Incompatible types: got "<address of procedure(TObject);Register>" expected "<procedure variable type of procedure(TObject) of object;Register>"

As the message says you can't use simple procedures you need to use object methods.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

ciammaruca

  • New Member
  • *
  • Posts: 20
Re: Problem with calling procdeure from different Units
« Reply #22 on: October 24, 2014, 08:46:47 am »
hi,
ok, but what do you mean? Have i to create an object, as described in this point:
http://wiki.freepascal.org/Programming_Using_Objects#Objects_-_Basics
But again, following my simple example, how to create and call an OnClick event in/from Unit2 of a runtime created object?
Plaese, could you show me the way?
 muchas gracias

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Problem with calling procdeure from different Units
« Reply #23 on: October 24, 2014, 08:53:08 am »
Regular procedures are declared OUTSIDE class declaration. "Procedure of object" or simply "method", is declared INSIDE class declaration. Depending on the problem context, you can put your own method in existing classes or write a new class and put it there instead. Technically, the difference lies in the hidden Self parameter. For methods, this refers to the current instance of the owning class. For procedures, this doesn't exist (well, what instance of what class should it refer to?) and thus, the two need to be considered different.

 

TinyPortal © 2005-2018