【问题标题】:In vb 6.0, how can we know if a dynamic array have been initialized? [duplicate]在 vb 6.0 中,我们如何知道动态数组是否已经初始化? [复制]
【发布时间】:2012-05-07 16:06:41
【问题描述】:

可能重复:
How do I determine if an array is initialized in VB6?
How do I check for an object being Nothing in VB6?

在函数返回动态数组作为结果的情况下,动态数组可能未初始化(例如,执行错误)。是否可以检查这种情况?

函数 IsNothing() 不起作用,并且 UBound() 在这种情况下会产生错误。

例如:

Function find(results() As String)

    [Definition here...]

End Function

[...]

Dim results() As String
find(results)
If UBound(results) > 0 Then '<-- This line will fail when results was not defined

[...]

提前致谢!

【问题讨论】:

  • @finnw 这是重复的,但不是您选择的问题!

标签: arrays vb6


【解决方案1】:

我遇到了同样的问题,但无法找到一个干净的方法来做到这一点。我最终创建了自己的函数来实现用错误处理程序包装的 Ubound。如果失败,我返回 -1。

Private Function custom_UBound(ByRef ToTest() As String)
    On Error GoTo errHandler

    custUBound = UBound(ToTest)

    Exit Function
errHandler:
    custUBound = -1
End Function

【讨论】:

  • 还有其他几种方法可以做到这一点,请参阅the duplicate question
  • 感谢对我来说很完美,谢谢!抱歉,我刚刚看到其他相关问题。起初我没有找到它们。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-04
  • 1970-01-01
相关资源
最近更新 更多