如何检查变量数组是否未分配? [英] How to check whether a variant array is unallocated?

查看:98
本文介绍了如何检查变量数组是否未分配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

   Dim Result() As Variant

在我的监视窗口中,它显示为

In my watch window, this appears as

Expression | Value | Type
Result     |       | Variant/Variant()

如何检查以下内容:

   if Result is nothing then

   if Result is Not Set then

这基本上是我要完成的工作,但是第一个不起作用,第二个不存在.

This is basically what I am trying to accomplish, but the first one does not work and the second does not exist.

推荐答案

Chip Pearson编写了一个有用的模块,称为 modArraySupport 包含一堆用于测试类似情况的函数.对于您的情况,您想使用IsArrayAllocated.

Chip Pearson made a useful module called modArraySupport that contains a bunch of functions to test for things like this. In your case, you would want to use IsArrayAllocated.

Public Function IsArrayAllocated(Arr As Variant) As Boolean

此函数返回TRUE或FALSE,指示是否分配了指定的数组(不为空).返回的TRUE array是已通过Redim语句分配的静态数组或动态数组.如果数组是一个动态数组,则返回FALSE 尚未使用ReDim调整大小或已使用Erase语句取消分配.此功能基本上与 ArrayIsEmpty.例如,

This function returns TRUE or FALSE indicating whether the specified array is allocated (not empty). Returns TRUE of the array is a static array or a dynamic that has been allocated with a Redim statement. Returns FALSE if the array is a dynamic array that has not yet been sized with ReDim or that has been deallocated with the Erase statement. This function is basically the opposite of ArrayIsEmpty. For example,

Dim Result() As Variant
Dim R As Boolean
R = IsArrayAllocated(Result)  ' returns false
ReDim V(1 To 10)
R = IsArrayAllocated(Result)  ' returns true

所使用的技术基本上是测试数组边界(如@Tim Williams所建议的),但要多加一些技巧.

The technique used is basically to test the array bounds (as suggested by @Tim Williams) BUT with an extra gotcha.

要在您的直接窗口中进行测试,请执行以下操作:

To test in your immediate window:

?IsArrayAllocated(Result)

在监视"窗口中进行测试:可以通过多种方式进行;例如,在R上添加一个监视,然后在监视类型"下选择值更改时中断".

Testing in Watch window: there are may ways to do this; for example, add a watch on R and under "Watch Type" select "Break When Value Changes".

这篇关于如何检查变量数组是否未分配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆