vb6 数组,上限为 -1
如果数组没有项,某些函数(例如 Split()
)将返回一个数组,其中 -1 为上限,0 为下限,如果数组没有项,例如:
Dim s() As String
s = Split("", ",")
Debug.Print UBound(s)
Debug.Pring LBound(s)
在这种情况下 UBound(s) 将等于 - 1 和 LBound(s) 将等于 0。我有相当多的代码检查上限是否为 -1,以查看数组是否有值。这很好用。
问题是我现在想将数组数据类型从字符串更改为长整型。我似乎无法创建一个上限为 -1 、下限为 0 的 long 数组,并且 Split()
和 Join()
函数只能在字符串数组。
我希望能够返回一个上限为-1 的长数组。这可能吗?
Some functions such as Split()
will return an array with -1 for the upper bound and zero for the lower bound if the array has no items, eg:
Dim s() As String
s = Split("", ",")
Debug.Print UBound(s)
Debug.Pring LBound(s)
In this case UBound(s) will equal -1 and LBound(s) will equal 0. I have a fair amount of code checking for -1 on the upper bound to see if the array has values or not. This works great.
The problem is that I now want to change the array data type from string to long. I cannot seem to create an array of longs with an upper bound of -1 and a lower bound of 0, and the Split()
and Join()
functions only operate on string arrays.
I would like to be able to return a long array with an upper bound of -1. Is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不认为你可以在 VB6 中自己完成它。但是,如果您愿意使用 Windows API 函数SafeArrayCreateVector,您可以做到:
如果您需要对其他数据类型执行此操作,您可以更改 vt 参数。
请注意,我想我最初是从 Vi2 对此的回答中发现这一点的 讨论。
I don't think you can do it in VB6 it self. However, if you're willing to use the Windows API function SafeArrayCreateVector you can do it:
If you need to do it for other datatypes you can change the vt parameter.
Please note, I think I originally found out about this from Vi2's answer to this discussion.
您可以编写自己的 split 函数来执行此操作:
You could write your own split function to do this:
VB6 的一个问题是无法可靠地创建或检测空(或未初始化)数组。有时,可以通过检查上限是否大于下限来检测未初始化的数组;然而,这既不优雅也没有记录。正确完成此类操作的最佳方法是将数组包含在 Variant 中,并将 Variant 设置为 Empty 以取消初始化数组。然后,您可以使用诸如 If VarType(v) = vbEmpty ... 之类的检查
One problem with VB6 is there is no way to reliably create or detect an empty (or uninitialized) array. Sometimes, it is possible to detect an uninitialized array by checking whether the upper-bound is greater than the lower-bound; however, this is neither elegant nor documented. The best way to accomplish such a thing properly is to enclose the array in a Variant, and set the Variant to Empty to deinitialize the array. You may then use a check such as If VarType(v) = vbEmpty ...
另一种方法是强类型的“工厂”函数:
我认为这也适用于整数类型。
Another way is a strongly typed "factory" function:
I think this also works with integral types.