明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 3374|回复: 9

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

[复制链接]
发表于 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
 楼主| 发表于 2004-3-7 21:53:00 | 显示全部楼层
赋过了,我写道省略.


问题是type放在sheet1中说不能,放在模块中则可以.然后重定义a()大小在sheet1中可以,而在模块中又不可以.该怎么办呢,type放在模块,重定义a()在sheet1中还是不行.
发表于 2004-3-8 08:55:00 | 显示全部楼层
我有另一种方法:将用户定义类型改为类模块。作法:添加一个类模块vvvv,将Public name As String
Public firstrow As Integer
Public lastrow As Integer
写入 引用时使用 dim a() as new vvvv
 楼主| 发表于 2004-3-8 20:17:00 | 显示全部楼层
非得要占用一个类模块这么麻烦么?大家平时的自定义类型是怎么用的?
发表于 2004-3-8 20:52:00 | 显示全部楼层
自定义类型怎么用,看我在2楼的贴。
 楼主| 发表于 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
Private a() 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
发表于 2012-12-26 09:48:28 | 显示全部楼层
my_computer 说的对,不过要把public 改为private
发表于 2012-12-30 00:04:44 | 显示全部楼层
type可以定义字符类型以外的类型吗?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-11-25 16:03 , Processed in 0.190336 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表