Wawan Ma-chun
Wawan Ma-chun

Reputation: 115

Sending/Passing local variable from one procedure to another in Delphi 7?

How can i send/passing local variable in a procedure to another procedure in delphi?

procedure TForm1.Button1Click(Sender: TObject);
var
a,b:integer;
c: array [o..3] smallint;
begin
  a:=1;
  b:=2;
end;

i want to send one or more local variable(a,b,c) that already has value to another procedure to use them there like:

procedure TForm1.Button2Click(Sender: TObject);
var
  d:integer;
begin
  d:=a*b;
end;

Upvotes: 1

Views: 1475

Answers (1)

David Heffernan
David Heffernan

Reputation: 612834

I want to send one or more local variable(a,b,c) that already has value to another procedure to use them there.

This shows a misunderstanding about the lifetime of local variables. Local variables only have scope for the duration of the function that owns them. Since your two event handlers have disjoint lifetimes, their local variables are never in existence simultaneously.

So when you say "that already has value", you are mistaken. The local variables that exist when Button1Click is executing simply do not exist when Button2Click is executing.

You'd need the variables to be members of the class rather than be local variables. That way the variables' lifetimes span the separate execution of your event handlers.

type
  TForm1 = class(TForm)
  ....
  private
    a,b:integer;
    // etc.
  end;
....
procedure TForm1.Button1Click(Sender: TObject);
begin
  a:=1;
  b:=2;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  d:integer;
begin
  d:=a*b;
end;

Upvotes: 7

Related Questions