数组的函数IsEmpty() [英] Function IsEmpty() for Array

查看:100
本文介绍了数组的函数IsEmpty()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想测试我的数组是否为空.

I would like test if my array is empty or not.

我尝试使用函数IsEmpty(),但是该函数始终返回False.并且不要输入if条件.

I tried to use the function IsEmpty(), but the function returns always False. And don't enter in the if condition.

我的下面的代码:

Sub Freeze()

    Dim tab_freeze() as variant

    If IsEmpty(tab_freeze) Then
        ReDim tab_freeze(0)
    Else
        ReDim Preserve tab_freeze(UBound(tab_freeze) + 1)
        tab_freeze(UBound(tab_freeze)) = "As you want here"
     End If

End Sub

我想让函数知道我的数组tab_freeze是否为空. 或者了解为什么IsEmpty(tab_freeze)第一次返回False.

I would like function to know if my array tab_freeze is empty or not. Or understand why IsEmpty(tab_freeze) returns False the first time.

推荐答案

为什么不使用用户Merri在此 vbforums线程,而不是使用Variant替换类型的数组Variant.

Why not use a little VB magic the user Merri talked about in this vbforums thread instead of using a Variant to replace an Array of type Variant.

处理数组时的常见问题之一是知道何时初始化数组以及何时未初始化数组.幸运的是,有一个简单的本地VB代码解决方案:

One of the common problems when dealing with arrays is to know when the array has been initialized and when it isn't. Luckily there is an easy native VB code solution:

Not Not ArrayName

这是要在数组变量中获取32位指针值,对位进行镜像,然后再次对其进行镜像.现在,您有效地知道了指向安全数组结构的指针.作为副作用,您还知道是否可以使用LBound和UBound访问该数组.

What this does is to take the 32-bit pointer value in the array variable, mirror the bits, and then mirror them again. You now effectively know the pointer to the safe array structure. As a side effect you also know whether you can access the array with LBound and UBound.

Private Sub ArrayTest()
    Dim testArr() as Variant
    ' Prints False
    Debug.Print Not Not testArr

    ReDim testArr(0)
    ' This prints True
    Debug.Print Not Not testArr
End Sub

Merri还谈到了可能发生的问题,但我还没有发现,我们必须记住,该文章是关于Visual Basic而非Visual Basic for Applications的. (介意,我在解释)
显然,在某些情况下,使用此方法会引发错误,可以通过在VB IDE中运行一次简单的Dim IDEbug() As Long: Debug.Assert Not IDEbug Or App.hInstance来避免该错误.
再次报价:

Merri also talked about problems that could occur, which I haven't stumbled upon yet, and we have to keep in mind that the post is about Visual Basic and not Visual Basic for Applications. (Mind you, I'm paraphrasing)
Apparently in some situations an error will be raised when using this method, which can be circumvented by running a simple Dim IDEbug() As Long: Debug.Assert Not IDEbug Or App.hInstance once before in the VB IDE.
Quoting again:

这里发生的是,首先我们打电话来推高可能的错误情况.然后我们调用App.hInstance:我们几乎可以对任何VB方法进行调用,但是由于hInstance返回Long数,因此我们使用它.无论出于何种原因,这都会使以下所有Not ArrayName调用均正常工作.在IDE中执行该行之后,您甚至可以注释该行,并且其效果仍然保留.仅关闭VB并再次打开它会重置条件,以便您需要再次拨打电话.

What happens here is that first we make a call to push up the possible error condition. Then we make a call to App.hInstance: we could make a call pretty much to any VB method, but since hInstance returns a Long number we use that. This, for whatever reason, makes all the following Not ArrayName calls work flawlessly. After the line has once executed in the IDE you can even comment the line and it's effects still remain. Only closing VB and opening it again will reset the condition so that you need to make the call again.

我想强调一点,我没有提出这个解决方案,但是它极大地帮助了我.我还想指出,我从未执行过Debug.Assert Not someArray Or Application.Hinstance并且还没有遇到任何问题.显然这可能与以下事实有关:VBA与VB略有不同,但我不能肯定地说.

I want to emphasize that I did not come up with this solution, but it helped me tremendously. I also want to point out that I never executed the Debug.Assert Not someArray Or Application.Hinstance and have yet to encounter any issues. This might obviously be related to the fact that VBA is a tiny bit different from VB, but I can't say that for sure.

这篇关于数组的函数IsEmpty()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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