我如何确定一个数组在VB6中初始化? [英] How do I determine if an array is initialized in VB6?

查看:279
本文介绍了我如何确定一个数组在VB6中初始化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

传递一个undimensioned数组给VB6的UBound函数会产生错误,所以我要检查它是否已尺寸还试图检查其上限之前。我该怎么做呢?

Passing an undimensioned array to the VB6's Ubound function will cause an error, so I want to check if it has been dimensioned yet before attempting to check its upper bound. How do I do this?

推荐答案

这就是我跟去了。这类似于GSerg的<一href=\"http://stackoverflow.com/questions/183353/how-do-i-determine-if-an-array-is-initialized-in-vb6#183668\">answer,但使用更好地记录CopyMemory的API函数,是完全独立的(你可以传递数组,而不是ArrPtr(数组)此功能)。它使用VarPtr函数,微软警告不要,但是这是一个只有XP的应用程序,它的工作原理,所以我不关心。

Here's what I went with. This is similar to GSerg's answer, but uses the better documented CopyMemory API function and is entirely self-contained (you can just pass the array rather than ArrPtr(array) to this function). It does use the VarPtr function, which Microsoft warns against, but this is an XP-only app, and it works, so I'm not concerned.

是的,我知道这个函数将接受任何你扔它,但我会离开错误检查作为读者练习。

Yes, I know this function will accept anything you throw at it, but I'll leave the error checking as an exercise for the reader.

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
  (pDst As Any, pSrc As Any, ByVal ByteLen As Long)

Public Function ArrayIsInitialized(arr) As Boolean

  Dim memVal As Long

  CopyMemory memVal, ByVal VarPtr(arr) + 8, ByVal 4 'get pointer to array
  CopyMemory memVal, ByVal memVal, ByVal 4  'see if it points to an address...  
  ArrayIsInitialized = (memVal <> 0)        '...if it does, array is intialized

End Function

这篇关于我如何确定一个数组在VB6中初始化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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