Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!mips!cs.uoregon.edu!ogicse!intelhf!ichips!iwarp.intel.com!pdxgate!eecs!mwizard From: mwizard@eecs.cs.pdx.edu (Craig Nelson) Newsgroups: comp.lang.pascal Subject: Re: Pointers to procedures Message-ID: <2301@pdxgate.UUCP> Date: 10 Apr 91 08:44:02 GMT References: <26505@adm.brl.mil> Sender: news@pdxgate.UUCP Lines: 108 CDCKAB%EMUVM1.BITNET@cunyvm.cuny.edu ( Karl Brendel) writes: >In article <9104081439.aa02828@VGR.BRL.MIL>, 209507097@ucis.vill.edu > (MCREE, JAMES F) wrote: >> I been following the thread of calling procedures by >>pointers with a lot of interest recently. Mostly because I have an >>application that is just begging for a solution like that. However, >>the discussion and samples that I have seen (including those in the >>manual) deal with hardcoding procedure names into the program. (ie. >>Proc[i] := Do_It_Procedure) Is there a way of doing what my >>syntactically incorrect program below is trying to do? >> >>Program Call_A_Variable_Procedure (Input,Output); >>. >>Var >> Proc_Name : Procedure; >>. >>. >>Assorted necessary delcarations >>. >>Begin >>. >>. >>Assorted code statements >>. >>. >>Write ('Enter the name of a procedure to run: '); >>Readln (Proc_Name); >>Proc_Name; >>. >>. >>End. >> >> >>This is an example of a user-specified call to a procedure. The >>user specifies the procedure directly though. Can this be done? >It isn't really apparent (to me) that your application is truly >"begging for a solution like [pointers to procedures]". Why can you >not just use a series of IF statements for your purpose? > Proc_Name := stUpCase(Proc_Name); > IF Proc_Name = 'PROC_A' THEN Proc_A > ELSE IF Proc_Name = 'PROC_B' THEN ProcB > etc.... How about this: {$F+} procedure proc1; begin writeln('In Proc1'); end; procedure proc2; begin writeln('In Proc2'); end; procedure proc3; begin writeln('In Proc3'); end; Const NumProcedures = 3; Type ProcString = String[10]; ProcRec = Record Name:PString; Proc:Procedure; end; Const MyProcedures : Array[1..NumProcedures] of ProcRec = ( (Name:'PROC1';Proc:proc1), (Name:'PROC2';Proc:proc2), (Name:'PROC3';Proc:proc3)); With this kind of setup you should be able to have a function that runs the procedure desired if found from the input line. For instance, if the function returned and index if the proper procedure was found, and 0 on an error, then i:= FindProcedure(ReadString); if (i>0) then MyProcedures[i].Proc else writeln('No such procedure'); Am I missing something or will this work ? Should.... Looks like it will. Cheers! []====================================================================[] || Craig R. Nelson | CCSofD Software Inc. || || Programmer | Beaverton, OR, 97005 || || mwizard@eecs.ee.pdx.edu | (unlisted on the net) ||