为了账号安全,请及时绑定邮箱和手机立即绑定

vb 让 函数返回一个集合?

vb 让 函数返回一个集合?

API
大话西游666 2019-02-14 15:11:20
自定义了一个函数,因为这个函数要返回多个 值,不可能定义成数组arr() ,然后arr(0)=? ,arr(1)=? 这样一个一个赋值 ,我试着用了一个集合 dim a as new collection然后在这个集合里面添加我要的返回值a.add " 信息1"a.add "信息2"················· ,那 能不能让这个函数的返回值是 a这个集合, 我想在其他地方调用这个函数,然后判断 函数返回的 a集合 里面的值 ,如何返回个集合
查看完整描述

2 回答

?
慕慕森

TA贡献1856条经验 获得超17个赞

'方法1
Function GetCollection1() As Collection
Dim col As Collection
Set col = New Collection
col.Add "信息1"
col.Add "信息2"
Set GetCollection1 = col
Set col = Nothing
End Function

'方法2
Sub GetCollection2(col As Collection)

Set col = New Collection
col.Add "信息3"
col.Add "信息4"

End Sub

Private Sub Command1_Click()

Dim a As Collection
Dim b

Set a = GetCollection1
For Each b In a
Debug.Print b
Next

Call GetCollection2(a)
For Each b In a
Debug.Print b
Next

End Sub



查看完整回答
反对 回复 2019-03-25
?
慕容森

TA贡献1853条经验 获得超18个赞

Private Type ABC '自定义数据类型
arr() As Variant '数组
a As New Collection '集合
End Type
Private Function f() As ABC '函数返回值是自定义类型
ReDim f.arr(3) '定义数组上界
For i = 0 To UBound(f.arr)
f.arr(i) = i '数组赋值
Next i
f.a.Add "信息1" '集合赋值
f.a.Add "信息2"
End Function
Private Sub Form_Click()
Dim x As ABC
x = f '调用函数
For i = 0 To UBound(x.arr)
Print x.arr(i) '输出数组
Next i
For Each i In x.a
Print i '输出集合
Next
End Sub



查看完整回答
反对 回复 2019-03-25
  • 2 回答
  • 0 关注
  • 644 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信