本文介绍了VB6阵列-1上限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

某些功能,如斯普利特()将与-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)

在这种情况下,UBound函数(S)将等于-1和LBOUND(S)将等于0。我有$ C $的c检查-1上限相当数量看,如果数组有值或不。这个伟大的工程。

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.

问题是,我现在想从字符串数组数据类型更改为长。我似乎无法建立多头的数组,数组的上限-1和下限为0,而斯普利特()加入( )功能只对字符串数组进行操作。

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.

我希望能够与上限的-1回到多头排列。这可能吗?

I would like to be able to return a long array with an upper bound of -1. Is this possible?

推荐答案

我不认为你可以在VB6它自已做。不过,如果你愿意使用Windows API函数你能做到这一点:

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:

Private Declare Function LongSplitEmulator Lib "OLEAUT32.DLL" Alias "SafeArrayCreateVector" _
    (Optional ByVal vt As VbVarType = vbLong, _
     Optional ByVal low As Long = 0, _
     Optional ByVal count As Long = 0) As Long()

Dim a() As Long
a = LongSplitEmulator()
MsgBox UBound(a)

如果你需要做它为其它数据类型可以改变VT参数。

If you need to do it for other datatypes you can change the vt parameter.

请注意,我想我最初发现了这个来自Vi2的的回答这个的。

Please note, I think I originally found out about this from Vi2's answer to this discussion.

这篇关于VB6阵列-1上限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 17:41