如何动态调用自定义函数?(200分)

D

dj

Unregistered / Unconfirmed
GUEST, unregistred user!
&nbsp;我在系统中把函数名放在了后台数据库的table中,然后在程序中对各函数<br>进行了定义.我想在主程序中通过一段控制过程动态的根据用户的输入从数据<br>库table中取出函数名然后运行.但不知如何实现,请救苦救难的大虾帮在下一<br>把.多谢!!!!!!!!
 
一个替代办法:<br><br>将函数体在程序中设定好,其功能由一个特定的index决定;将index放入数据库;根据用户的输入翻译成index,再用case在程序中实现不就行了?
 
你的问题还是关于宏替换的问题,无法实现。完全可以通过参数传递实现你的要求!<br>象Schiesser说的那样。<br><br>procedure myproc(index:integer);<br>begin<br>&nbsp;...<br>&nbsp;case index of <br>&nbsp; 1://proc 1;<br>&nbsp; 2://proc 2;<br>&nbsp; ...<br>&nbsp;end;<br>end;<br><br>在程序中调用:<br><br>...<br>myproc(table1.fieldbyname('index').asinteger);<br>...<br><br>
 
对,直接用函数名是不行的。<br>你还可以用一个联合类型,如:<br>&nbsp; TMyFunc = (func1,func2,func3);<br>&nbsp; <br>&nbsp; 然后定义一个数组:<br>&nbsp; ArryFunc:=array [func1..func3] of pointer;<br><br>然后再用。 &nbsp;
 
直接用函数名可以的:<br>举例:<br>将procedure abc定义在form的&lt;font color=red&gt;publish&lt;/font&gt;部分,然后:<br><br>procedure TForm1.abc;<br>begin<br>&nbsp;showmessage('ok');<br>end;<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>var f:procedure;<br>begin<br>f:=self.MethodAddress('abc');<br>f;<br>end;<br>
 
我们要求实现类foxprox宏替换(&amp;)的形式.<br>因此回答问题不接受.<br>请另外提供方法.
 
'publish',是否是PUBLIC? 使用MethodADDress方法不行,出现错误Access violation at address 00009 ,write of ad 70452980,请<br>,请给出新的方法
 
请问 F 是否能定义成函数,用于传递参数?
 
我的例子就是实现你的功能的呀!再给你写的明白点吧!<br><br>用这个函数实现你的功能,参数是你要调用的函数名(前提是被调用函数必须定义在<br>publish部分)<br><br>procedure MRun(FuncName:string)<br>var f:procedure;<br>begin<br>&nbsp; f:=self.MethodAddress(FuncName);<br>&nbsp; f; &nbsp;<br>end;
 
你好:<br>&nbsp; 请问在你的回答中,f在PUBLISH的定义中是否可以procedure或function,如果<br>是函数,那能否传递参数,请问如何实现?<br>&nbsp; 我们用上述方法,对于procedue,可以实现,而定义成函数时,出现错误<br>Access violation at address 00009 ,write of ad 70452980,<br>请给于回答.如可能请提供详细的源代码的例子,谢谢!!!<br>
 
这样的思路:<br><br>function foxmicro(s:string,k:array of varient):varient;<br>var<br>&nbsp; f:function;<br>&nbsp; i:integer;<br>&nbsp; function test1(i:integer,j:integer):integer;<br>&nbsp; begin<br>&nbsp; &nbsp; result:=i+j;<br>&nbsp; end;<br>begin<br>&nbsp; f:=self.methodaddress(s);<br>&nbsp; result:=nil;<br>&nbsp; if f&lt;&gt;nil then<br>&nbsp; begin<br>&nbsp; &nbsp; i:=high(k);<br>&nbsp; &nbsp; select i<br>&nbsp; &nbsp; case 0:<br>&nbsp; &nbsp; &nbsp; result:=f;<br>&nbsp; &nbsp; case 1:<br>&nbsp; &nbsp; &nbsp; result:=f(k[0]);<br>&nbsp; &nbsp; case 2:<br>&nbsp; &nbsp; &nbsp; result:=f(k[0],k[1]);<br>&nbsp; &nbsp; .....<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>end;<br>
 
FUNCTION数据类型不存在,编译错误,能否给出已编译成功的实例,<br>最好是比较完整的程序
 
呵呵, 换procedure试试:)
 
我们需要定义函数类的变量,急需!!!
 
顶部