dim s() as string dim i as long for i=1 to 10 redim Preserve s(i) s(i)="aaa" & i next i for i=0 to ubound(s) debug.print s(i) next i
Public tcType() As tTypeType tType tID As Integer tName As String tPlace As String End TypePublic Sub AddType(pID As Integer, pName As String, pPlace As String) //增加数组 ReDim Preserve tcType(UBound(tcType) + 1) As tType tcType(UBound(tcType)).tID = pID tcType(UBound(tcType)).tName = pName tcType(UBound(tcType)).tPlace = pPlace End SubPublic Sub ClearType() //清除数组 ReDim tcType(0) As tType End Sub
难道没有像C中的 UserType myData[]; 这种方式的?
谢谢tommychim(大脚鸟),你的代码很有启发性!
该示例使用 ReDim 语句为动态数组变量分配和重分配存储空间。假设 Option Base 是 1。Dim MyArray() As Integer '声明动态数组。 Redim MyArray(5) '分配 5 个元素。 For I = 1 To 5 '循环 5 次。 MyArray(I) = I '初始化数组。 Next I下一条语句重定义该数组的大小,并清除其中的元素。Redim MyArray(10) '大小重定为 10 个元素。 For I = 1 To 10 '循环 10 次。 MyArray(I) = I '初始化数组。 Next I下面的语句重定义该数组的大小,但没有清除其中的元素。Redim Preserve MyArray(15) '大小重定为 15 个元素。
需要动态调用时 redim MyArray(9)
dim i as long
for i=1 to 10
redim Preserve s(i)
s(i)="aaa" & i
next i
for i=0 to ubound(s)
debug.print s(i)
next i
tID As Integer
tName As String
tPlace As String
End TypePublic Sub AddType(pID As Integer, pName As String, pPlace As String)
//增加数组
ReDim Preserve tcType(UBound(tcType) + 1) As tType
tcType(UBound(tcType)).tID = pID
tcType(UBound(tcType)).tName = pName
tcType(UBound(tcType)).tPlace = pPlace
End SubPublic Sub ClearType()
//清除数组
ReDim tcType(0) As tType
End Sub
UserType myData[];
这种方式的?
Redim MyArray(5) '分配 5 个元素。
For I = 1 To 5 '循环 5 次。
MyArray(I) = I '初始化数组。
Next I下一条语句重定义该数组的大小,并清除其中的元素。Redim MyArray(10) '大小重定为 10 个元素。
For I = 1 To 10 '循环 10 次。
MyArray(I) = I '初始化数组。
Next I下面的语句重定义该数组的大小,但没有清除其中的元素。Redim Preserve MyArray(15) '大小重定为 15 个元素。