Dim x As Integer
Dim y As Integer
Dim intSum As Integer
Function Sum(ParamArray intNums())
For Each x In intNums
y = y + x
Next x
intSum = y
End FunctionPrivate Sub Command1_Click()
Sum 1, 3, 5, 7, 8
List1.AddItem intSum
End Sub这是我在书上看到的代码
但提示  For Each 数组的控件变量必须为变体
各位请帮我看看是哪儿的问题.

解决方案 »

  1.   

    这本书有问题了....
    你的sum函数用了关键字ParamArray,这样参数可以不定数量,但intNums作为Variant数组,它有成员当然只能是Variant,你的这个代码中,x是一个全局的integer,自然不对:
    'Dim x As Integer 
    'Dim y As Integer 
    Dim intSum As Integer Function Sum(ParamArray intNums()) 
        dim x
        dim y as integer
        For Each x In intNums 
            y = y + x 
        Next x 
        intSum = y 
    End Function Private Sub Command1_Click() 
        Sum 1, 3, 5, 7, 8 
        List1.AddItem intSum 
    End Sub 
      

  2.   

    LS正解。把x,y定义到函数里面。
      

  3.   


    Dim x As Integer
    Dim y As Integer
    Dim intSum As IntegerSub Sum (ParamArray intNums ())
       For Each x In intNums
          y = y + x
       Next x
       intSum = y
    End SubPrivate Sub Command1_Click ()
       Sum 1, 3, 5, 7, 8
       List1.AddItem intSum
    End Sub这是原文,MSDN里这个x并非integer那个变量,而是List1的Items。
      

  4.   

    LS的说错了,原来MSDN不是那样用的……刚才没仔细看,实在抱歉。
      

  5.   

    查到了,果然是MSDN的示例:-------------------------------------------------------------------------------------
    使用不定数量的参数
    一般说来,过程调用中的参数个数应等于过程说明的参数个数。可用 ParamArray 关键字指明,过程将接受任意个数的参数。于是可以这样来编写计算总和的 Sum 函数:Dim x As Integer
    Dim y As Integer
    Dim intSum As IntegerSub Sum (ParamArray intNums ())
       For Each x In intNums
          y = y + x
       Next x
       intSum = y
    End SubPrivate Sub Command1_Click ()
       Sum 1, 3, 5, 7, 8
       List1.AddItem intSum
    End Sub-------------------------------------------------------------------------------------
      

  6.   

    可是我用LS的试了一下 
    依然报错
    难到是我的VB没安装好??一楼的回答到是解决了问题
      

  7.   

    For each in 是针对某一种类型的对象集合,类似与遍历集合中所有的变量
      

  8.   

    1#当然能解决问题....
    这是MSDN的错误之一...MSND的错误不是一个人二个抱怨了.....