gpt4 book ai didi

arrays - 如何制作 UBound < LBound 的 VB6 变体数组?

转载 作者:行者123 更新时间:2023-12-01 15:23:40 35 4
gpt4 key购买 nike

我试图摆脱 VB6 应用程序中对 SCRRUN.DLL 的依赖。它目前正在使用的其中一件事是它的 Dictionary 类。 Dictionary 类有一个 Keys 函数,它应该返回字典中键的数组。我做了一些实验,看看如果字典中没有键会发生什么:

Dim D As Dictionary
Set D = New Dictionary
Dim K() As Variant
K = D.Keys
MsgBox LBound(K) & ", " & UBound(K)

我期待“下标超出范围”或类似的东西,但我却被告知 LBound 是 0 而 UBound 是 -1。

那么,如何创建一个具有 LBound 0 和 UBound -1 的 Variant 数组?

我试过只使用一个未初始化的变体数组:

Dim K() as Variant
MsgBox LBound(K) & ", " & UBound(K)

当然,正如我所料,这会抛出“下标超出范围”。删除未初始化的数组也是如此:

Dim K() as Variant
Erase K
MsgBox LBound(K) & ", " & UBound(K)

就像删除初始化数组一样:

Dim K() As Variant
ReDim K(0 To 0)
Erase K
MsgBox LBound(K) & ", " & UBound(K)

我也试过将亮度调到 0 和 -1,这看起来很奇怪:

Dim K() As Variant
ReDim K(0 To -1)
MsgBox LBound(K) & ", " & UBound(K)

但这也会给出“下标超出范围”。

在网上翻了一下,我发现了以下技巧:

Dim K() As String
K = Split(vbNullString)
MsgBox LBound(K) & ", " & UBound(K)

这实际上确实给出了一个 LBound 0 和 UBound -1 的数组!不幸的是,它是一个 String 数组,而我需要一个 Variant 数组。我不能很好地将字符串从一个数组单独复制到另一个数组中的变体,因为,0 到 -1 等等。

有谁知道如何在不使用 SCRRUN.DLL 的情况下使用 LBound 0 和 UBound -1 创建这样的数组 Variant()?最好也只使用内置的 VB6 东西,但是如果你可以使用一些外部东西(除了 SCRRUN.DLL),我会洗耳恭听。谢谢。

最佳答案

您可以使用 Array功能:

Dim K()
K = Array()
MsgBox UBound(K)

关于arrays - 如何制作 UBound < LBound 的 VB6 变体数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20555025/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com