: :
This message was edited by king0deu at 2007-1-1 9:47:11
: : : : : : My problem is I can't assign a procedure to a variable within a procedure
: : : : : :
: : : : : : I kknow this may be confused so look at this:
: : : : : :
: : : : : :
: : : : : : type proc=procedure;
: : : : : :
: : : : : : procedure main;
: : : : : : var p:proc;
: : : : : :
: : : : : : procedure one;
: : : : : : begin
: : : : : : {something}
: : : : : : end;
: : : : : :
: : : : : : begin
: : : : : : p:=one;
: : : : : : end;
: : : : : :
: : : : : : begin
: : : : : : p;
: : : : : : end.
: : : : : :
: : : : : :
: : : : : : when you run that you'll encounter the error: invalid proceudre reference..
: : : : : : So is there a solution to use procedural vars within another procedure?
: : : : : : I need this be cause I want to put my whole program in a unit
: : : : : :
: : : : : : help me pls
: : : : : :
: : : : : :
: : : : : Hi !
: : : : : You must write :
: : : : :
@p:=@one;
: : : : :
: : : :
: : : : Thanks, man. It seemed to work, but there's another problem:
: : : : if I pass a parameter to the main procedure, the procedure one can't access it, like this:
: : : :
: : : :
: : : : : : var y:integer;
: : : :
: : : : : procedure main(var x:integer);
: : : : : : var p:procedure;
: : : : : :
: : : : : : procedure one;
: : : : : : begin
: : : : : : {something}
: : : : : : x:=123; {change the value of x which is a parameter}
: : : : : : end;
: : : : : :
: : : : : : begin
: : : : : : x:=0;
: : : : : : @p:=@one; {assign to variable}
: : : : : : p; {call the procedure one, to change x to 123}
: : : : : : end;
: : : : : :
: : : : : : begin
: : : : : : y:=1;
: : : : : : main(y);
: : : : : : end.
: : : :
: : : :
: : : : but the result is that y, after the call main(y), is still 1, it is not changed.
: : : : the procedure one is surely called (via the call of p), but it can't access x, I think so
: : : :
: : : : any idea?
: : : :
: : : Appearantly the x variable in One() is stored in the stack of One() instead of Main(), thus it remains unchanged. As for the value of y, it has been changed: to 0.
: : : Here is a working example:
: : :
: : : type
: : : TIntChanger = procedure(var Value: integer);
: : :
: : : procedure One(var Value: integer); far;
: : : begin
: : : Value := 1;
: : : end;
: : :
: : : procedure Two(var Value: integer); far;
: : : begin
: : : Value := 2;
: : : end;
: : :
: : : procedure main(var x: integer);
: : : var
: : : p: TIntChanger;
: : : begin
: : : @p := @One; {assign to variable}
: : : p(x); {call the procedure one, to change x to 123}
: : : end;
: : :
: : : var
: : : y: integer;
: : : begin
: : : y:=0;
: : : main(y);
: : : writeln(y);
: : : readln;
: : : end.
: : :
: : : As you can see I've taken One() procedure out of the Main() procedure (as previously suggested), and added it's own procedural type definition, since that is necessary in this case to make the change. To make sure the code worked, I've tested it also with the Two() procedure.
: : :
: :
: : yes, but, to make it handy, I want to put this pile of code in to a unit, that means I have to put the whole code in a procedure, that's my main problem.
: :
: : In addition, if I replace the call p() in the code with one(), ie. call the procedure directly, it worked, it can change the value of x parameter (to 123). But I dont know why calling p does not work
: :
: :
: :
: I just explained a probable cause, why calling P() doesn't work. Units can have more than one procedure/function/type definitions/variable declarations/etc.; in fact nearly all of them have. Here's the same program but then with a unit:
:
: unit ProcVar;
:
: interface
:
: procedure main(var x: integer);
:
: implementation
:
: type
: TIntChanger = procedure(var Value: integer);
:
: procedure One(var Value: integer); far;
: begin
: Value := 1;
: end;
:
: procedure Two(var Value: integer); far;
: begin
: Value := 2;
: end;
:
: procedure main(var x: integer);
: var
: p: TIntChanger;
: begin
: @p := @One; {assign to variable}
: p(x); {call the procedure one, to change x to 123}
: end;
:
: end.
:
:
: program PVProg;
:
: uses
: ProcVar;
:
: var
: y: integer;
: begin
: y:=0;
: main(y);
: writeln(y);
: readln;
: end.
:
:
I see.. thanks alot