mikewolf2k 发表于 2004-3-7 20:48:00

如何实现自定义类型的动态数组?

Type        aa
                                                               name As String
                                                               firstrow As Integer
                                                               lastrow As Integer
                       End Type
                       Dim a() As aa
                       ReDim Preserve a(0 To 2)
'将a(0)~a(2)赋值,语句省略
                       addtype a, "ok"
Sub addtype(a, str)
                       Dim i As Integer
                       ReDim Preserve a(UBound(a) + 1)
                       With a(UBound(a))
                                                               .name = str
                                                               .firstrow = a(UBound(a) - 1).lastrow
                                                               .lastrow = .firstrow
                       End With
End Sub
请帮忙解决以上代码的错误,实现能根据要求增加动态数组a()的功能

莫名 发表于 2004-3-7 21:09:00

数组a未赋初始值!
Private Type aa
                                                               name As String
                                                               firstrow As Integer
                                                               lastrow As Integer
                       End Type
       
Private Sub Command1_Click()
                       Dim a() As aa
                       Dim i As Integer, k As Integer
                       ReDim Preserve a(0)
                       a(0).firstrow = 1
                       a(0).lastrow = 2
                       a(0).name = "ok"
                       For i = 1 To 2
                       ReDim Preserve a(i)
                       With a(i)
                                                               .name = "str"
                                                               .firstrow = a(i - 1).lastrow
                                                               .lastrow = .firstrow
                                                               Debug.Print i, .name & "," & .firstrow & "," & .lastrow
                       End With
                       Next
End Sub

mikewolf2k 发表于 2004-3-7 21:53:00

赋过了,我写道省略.
问题是type放在sheet1中说不能,放在模块中则可以.然后重定义a()大小在sheet1中可以,而在模块中又不可以.该怎么办呢,type放在模块,重定义a()在sheet1中还是不行.

my_computer 发表于 2004-3-8 08:55:00

我有另一种方法:将用户定义类型改为类模块。作法:添加一个类模块vvvv,将Public name As String
Public firstrow As Integer
Public lastrow As Integer
写入
引用时使用
dim a() as new vvvv

mikewolf2k 发表于 2004-3-8 20:17:00

非得要占用一个类模块这么麻烦么?大家平时的自定义类型是怎么用的?

莫名 发表于 2004-3-8 20:52:00

自定义类型怎么用,看我在2楼的贴。

mikewolf2k 发表于 2004-3-8 20:54:00

请看3楼,我的type aa 要是全局变量.谢谢

莫名 发表于 2004-3-8 21:25:00

注意a要定义为全局变量或窗体的模块级变量。
新建窗体、添加命令按钮。将下面代码放到窗体中。
Private Type aa
       name As String
       firstrow As Integer
       lastrow As Integer
End Type
Privatea() As aa
       
Private Sub addtype(str)
                       Dim k As Integer
                       k = UBound(a, 1)
                       ReDim Preserve a(k + 1)
                       k = UBound(a, 1)
                       With a(k)
                                                               .name = str
                                                               .firstrow = a(k - 1).lastrow
                                                               .lastrow = .firstrow
                       End With
                       Debug.Print k, a(k).name, a(k).firstrow, a(k).lastrow
                       
End Sub
Private Sub Command1_Click()
                       
                       ReDim a(0)
                       a(0).firstrow = 2
                       a(0).lastrow = 2
                       a(0).name = "ok"
                       Dim i As Integer
                       
                       addtype "ok"
End Sub

asd_10000 发表于 2012-12-26 09:48:00

my_computer 说的对,不过要把public 改为private

crazylsp 发表于 2012-12-30 00:04:00

type可以定义字符类型以外的类型吗?
页: [1]
查看完整版本: 如何实现自定义类型的动态数组?