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

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

问题描述

将未标注的数组传递给 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 的 answer,但使用更好记录的 CopyMemory API 函数并且是完全独立的(您可以将数组而不是 ArrPtr(array) 传递给此函数).它确实使用了 VarPtr 函数,Microsoft 警告,但这是一个仅限 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天全站免登陆