争怎路由网/网站教程/内容

能否判断动态数组有没有被分配过

网站教程2024-06-22 阅读
 动态数组在VB语言中常能起到出奇制胜的作用。但数组有没有被重新定义或释放,用“is empty”,“is null”,“=" "”等方法都不起作用。所以判断时一般采用侦别错误消息(ON ERROR )的方法。下面给出一个非错误侦别的代码,判断动态字符串数组的分配情况:

 

Private Sub Command1_Click()
Dim a() As String, i As Long


MsgBox hasredim(a), 64, "Has a() been redimed?"   '未初始化


ReDim a(20)

For i = 1 To 20
a(i) = chr(i+64)

Next

MsgBox hasredim(a), 64, "Has a() been redimed?"     '初始化后


Erase a    '释放空间后
MsgBox hasredim(a), 64, "Has a() been redimed?"   


End Sub

 


Function hasredim(ByRef x() As String) As Boolean  '定义布尔函数
Dim temp As String
temp = Join(x, ",")
hasredim = LenB(temp) > 0 '空数组长度为零
End Function





……

相关阅读