首页上一页 1 下一页尾页 2 条记录 1/1页
关于数组参数的问题
发表在VB答疑区
2010-11-02
是否精华
是
否
版块置顶:
是
否
Private Function Average(age() As Integer, n As Integer) As Integer
'定义三个整型变量
Dim i As Integer, aver As Integer, sum As Integer
'使用循环语句求和
For i = 0 To n - 1
sum = sum + age(i)
Next i
'求平均数
aver = sum / n
Average = aver
End Function
Private Sub Command1_Click()
'定义一个动态数组用于存储员工年龄
Dim Employees() As Integer
ReDim Employees(6)
'给数组赋值
Employees(0) = 20: Employees(1) = 28: Employees(2) = 30
Employees(3) = 24: Employees(4) = 25: Employees(5) = 35
'调用求平均数的函数
Text1 = Average(Employees, 6)
End Sub
=============以下问题=======================
Dim Employees() As Integer
ReDim Employees(6)
书中说因为函数过程中的age()需要改变维数,因此Employees()必须定义成动态的数组。
我不这么认为呀,我将他试着定义成静态数组Dim Employees(6) As Integer
也可以完成呀。为什么一定要动态的呢,谁能给解释一下。
恳请各位高人给指点一二。小弟不胜感激
'定义三个整型变量
Dim i As Integer, aver As Integer, sum As Integer
'使用循环语句求和
For i = 0 To n - 1
sum = sum + age(i)
Next i
'求平均数
aver = sum / n
Average = aver
End Function
Private Sub Command1_Click()
'定义一个动态数组用于存储员工年龄
Dim Employees() As Integer
ReDim Employees(6)
'给数组赋值
Employees(0) = 20: Employees(1) = 28: Employees(2) = 30
Employees(3) = 24: Employees(4) = 25: Employees(5) = 35
'调用求平均数的函数
Text1 = Average(Employees, 6)
End Sub
=============以下问题=======================
Dim Employees() As Integer
ReDim Employees(6)
书中说因为函数过程中的age()需要改变维数,因此Employees()必须定义成动态的数组。
我不这么认为呀,我将他试着定义成静态数组Dim Employees(6) As Integer
也可以完成呀。为什么一定要动态的呢,谁能给解释一下。
恳请各位高人给指点一二。小弟不胜感激