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

来源:互联网 发布:杭州微米网络怎么样 编辑:程序博客网 时间:2024/04/30 09:23
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

       动态数组在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

<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击