PDA

View Full Version : Calling a procedure/function...



HopeDagger
16-02-2004, 07:44 PM
A simple and short question that deserves an equally short answer:

Is a procedure that can call a procedure/function?

E.g.


procedure TheProc;
begin
end;

var
p: string;
begin
p := 'TheProc';
magic_proc(p);
end.


I looked through the helpfile, although it's hard to find a command if you don't know what it's called. :/

Thanks!

Paulius
16-02-2004, 08:24 PM
You can declare a variable procedure, asign some other procedure to it and call it normally, like:
var
var
VariableProcedure: procedure;
VariableProcedure:=@Procedure1;
VariableProcedure;

HopeDagger
16-02-2004, 08:33 PM
Hm. Is there a way to do that by supplying a string as the proc name?

Here's what I was thinking, but (of course) it doesn't work:


procedure TForm1.Thing;
begin
Button1.Caption := 'Thing proc called.';
end;

procedure TForm1.Button1Click(Sender: TObject);
var
p: procedure;
a: string;
begin
a := 'Thing';
p := @a;

p;
end;

Paulius
16-02-2004, 09:15 PM
I don't think so. Why not use arrays of corresponding strings and function addresses, and get by by changing array's index value.

HopeDagger
16-02-2004, 09:23 PM
I don't think so. Why not use arrays of corresponding strings and function addresses, and get by by changing array's index value.

Because that would defeat the purpose of being able to call it by only needing a string. With that method that you described, I'd have to know all of the names of the procedures I want to call, and store them. That's what I'm trying to avoid. :)

Paulius
16-02-2004, 09:37 PM
Actually you'd still need stored string anyway, because no procedure names are compiled into the code and searching through strings woul'd be much slower.

HopeDagger
16-02-2004, 09:47 PM
Actually you'd still need stored string anyway, because no procedure names are compiled into the code and searching through strings woul'd be much slower.

I don't know what I'm going to do, then. :/

In my case, it wouldn't be efficient at all to have to name every procedure/function in the code itself. Any ideas?

Ultra
16-02-2004, 10:57 PM
If you look at this topic: http://terraqueous.f2o.org/dgdev/viewtopic.php?t=1178 Alimonster showed a way to do more or less what you want.

Also if your procedures/functions are in a dll you could call @myproc := GetProcAddress(PChar(myprocname));