Public Function tt(n As Integer, p As Integer) As Integer
    tt = n * p
End FunctionPrivate Sub Form_Load()
Dim cc As Integer
Dim p As Integer
Dim arrid(1) As Integer
Dim fun_name As String      '函数名称
Dim n As Integer             '参数
fun_name = "tt"
arrid(0) = 3
arrid(1) = 5
cc = CallByName(Form1, fun_name, VbMethod, arrid( ))
MsgBox cc
End Subcc = CallByName(Form1, fun_name, VbMethod, arrid( ))
为什么我这样来传递这个数组就不行,正确的要怎么传递?
谢谢..

解决方案 »

  1.   

    cc = CallByName(Form1, fun_name, VbMethod, 3, 5)
      

  2.   

    把这里的函数参数改改看看Public Function tt(a() as Integer) As Integer 
        tt = a(0) * a(1) 
    End Function
      

  3.   

    声明与调用要一致,直接调用 Form1.tt(arrid) 也是不行的。
      

  4.   

    dim s as string
    dim i as integer
    dim arr as variants="ABC"
    i=101
    arr=array(1,"D")cc = CallByName(Form1, fun_name, VbMethod, s, i,arr)
      

  5.   

    谢谢星星的师兄们!
    对了,家人:
    s="ABC"
    i=101
    arr=array(1,"D")cc = CallByName(Form1, fun_name, VbMethod, s, i,arr)这里面要怎么调用函数的?
      

  6.   

    对了,更奇怪的是,我下面这样也不行
    arraystr1(0) = "1"
    arraystr1(1)="2"
    cc = CallByName(Form1, fun_name, VbMethod, arraystr1(0), arraystr1(2))
    但一改成这样又可以了:
    a = "1"
    b ="2"
    cc = CallByName(Form1, fun_name, VbMethod, a, b)
    好像明显着不支持数组的.
      

  7.   

    我把全部的需求及数据库结构都贴出来,希望有高手指点一下。
    谢谢。
    数据库存储的数据如下,
    其中:function_name为函数名,function_value为函数格式,
    content_info为函数的具体参数值.id   function_name    function_value     content_info
    1    selecttype1        str,1                1;
    2    selecttype2        str                  2例如现在从数据库读出ID为1这条纪录的时候,
    会执行这个selecttype1(1;,1)的函数并把处理后的结果赋给另外一个变量vb_CallBy
    其中函数名(selecttype1)、函数格式(str,1)、函数参数值(1;)
    即是将function_value字段里面的"str,1"中的str替换成参数值里面的1;变为(1;,1),然后用这个函数selecttype1来执行,并把执行结果赋给另外一个变量vb_CallBy。
    因为数据库里面存储的是不同的函数格式及函数内容,所以仍需站在一个通用的高度上解决。
    再次谢谢!
      

  8.   

    selecttype1、selecttype2 这些都已经写好了,存在public里面。
    现在主要是读出来后怎么执行函数,并且不同的函数有不同的格式。
    要怎么进行包含考虑?
    谢谢
      

  9.   

    我本来是想来拆函数格式里面的“,"变成数组再进判断。
    但是,现在好像CallByName又认不了数组。
      

  10.   


    private sub from_load()
    print "test!"
    end sub
      

  11.   

    private sub from_load() 
    print "test!" 
    end sub
      

  12.   


    private sub from_load()  
    print "test!"  
    end sub 
      

  13.   

    将所有的函数都定义成 function selecttype1(args() as variant) 的形式,这样直接将 content_info 用 "," split 后的数组可以固定作为 CallByName 的第 4 个参数使用。
      

  14.   

    '你的问题没看明白,举个例子吧:
    '下面这个函数需要三个函数,一个string,一个integer,一个string数组
    Function test(s As String, n As Integer, arr() As String) As Integer
        Dim tmp As Integer, i As Integer
        tmp = Len(s) + n
        For i = 0 To UBound(arr)
            tmp = tmp + Len(arr(i))
        Next
        test = tmp
    End FunctionPrivate Sub Command1_Click()
        Dim strS As String     '参数s
        Dim intN As Integer    '参数n
        Dim arrS(1) As String  '参数arr
        Dim v As Integer
        
        strS = "ABC"
        intN = 1000
        arrS(0) = "D"
        arrS(1) = "FF"
        
        v = CallByName(Me, "test", VbMethod, strS, intN, arrS)
        MsgBox v
          
    End Sub
      

  15.   


    Option ExplicitPublic Function Test(intData() As Integer, intSize As Integer) As Integer
        Dim intLoop As Integer
        
        For intLoop = 0 To intSize
                Test = Test + intData(intLoop) 'Count
        Next
    End FunctionPrivate Sub Command1_Click()
        Dim intReturn As Integer     'Count
        Dim arrid(1) As Integer
        Dim fun_name As String       '函数名称
        Dim ArrSize  As Integer      'arrary Size
        fun_name = "Test"            'set Name of function
        ArrSize = UBound(arrid)      'set UpIndex of Arrary
        arrid(0) = 3
        arrid(1) = 5
        intReturn = CallByName(Me, fun_name, VbMethod, arrid(), ArrSize)
        MsgBox intReturn
    End Sub你把函数这样写,经过测试通过,经过为8
      

  16.   

    Option Explicit
    Public Function Test(intData() As Integer, intSize As Integer) As Integer
        Dim intLoop As Integer
        
        For intLoop = 0 To intSize
                Test = Test + intData(intLoop) 'Count
        Next
    End FunctionPrivate Sub Command1_Click()
        Dim intReturn As Integer     'Count
        Dim arrid(1) As Integer
        Dim fun_name As String       '函数名称
        Dim ArrSize  As Integer      'arrary Size
        fun_name = "Test"            'set Name of function
        ArrSize = UBound(arrid)      'set UpIndex of Arrary
        arrid(0) = 3
        arrid(1) = 5
        intReturn = CallByName(Me, fun_name, VbMethod, arrid(), ArrSize)
        MsgBox intReturn
    End Sub你把函数这样写,经过测试通过,经过为8
      

  17.   

    谢谢您们.
    嘿嘿,终于有点眉目了.
    不过,对了,ZOU_SEAFARER,我需要传送的数组类型是这样的,都是字符型来的.
    arrid(0)="1;" 
    arrid(1)="1"  这个要怎么改的?
      

  18.   

    Public Function arraytype(ByVal outdata1, outdata2)
    '其中outdata1接收的是"1;",outdata2接收的是"1"
    '之所以outdata1接收的是"1;"是因为有时候要传递的值可能是"1;2;3;",我在函数里面会有一个循环读取的程序。
    ***************代码太多了,暂先略掉。
    end sub
      

  19.   

    我把全部的需求及数据库结构都贴出来,希望有高手指点一下。 
    谢谢。 
    数据库存储的数据如下, 
    其中:function_name为函数名,function_value为函数格式, 
    content_info为函数的具体参数值. id   function_name    function_value     content_info 
    1    selecttype1        str,1                1; 
    2    selecttype2        str                  2 例如现在从数据库读出ID为1这条纪录的时候, 
    会执行这个selecttype1(1;,1)的函数并把处理后的结果赋给另外一个变量vb_CallBy 
    其中函数名(selecttype1)、函数格式(str,1)、函数参数值(1;) 
    即是将function_value字段里面的"str,1"中的str替换成参数值里面的1;变为(1;,1),然后用这个函数selecttype1来执行,并把执行结果赋给另外一个变量vb_CallBy。 
    因为数据库里面存储的是不同的函数格式及函数内容,所以仍需站在一个通用的高度上解决。 
    再次谢谢! ----------------------------------------------------------
    dim rs as adodb.recordset
    '......dim fun as string
    dim p1 as string,p2 as string
    dim vb_CallBy fun=rs!function_name '得到函数名
    if ubound(split(rs!function_value,","))>0 then
        '如果function_value有逗号
        p1=rs!content_info                '参数1就是rs!content_info          
        p2=split(rs!function_value,",")(1)'参数2是function_value中的逗号后面的值
    end ifvb_CallBy =callbyname(me,"selecttype1",vbmethod,p1,p2)这个意思?
     
      

  20.   

    vbman2003:谢谢您。
    被你这样一分,好像清淅很多呀。
    没错,只要能执行这个selecttype1(1;,1)函数就行了,要传递的参数是1;1这两个。
      

  21.   

    Dim arrid(1) As Integer  CHANGE to Dim arrid(1) As String
      

  22.   

    ZOU_SEAFARER 谢谢您!
    CHANGE to Dim
    这个是什么东东啊,我一加又就提示错了?
      

  23.   

    private sub from_load()  
    print "test!"  
    end sub