Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vb6 array with -1 for upper bound

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?

like image 934
bugtussle Avatar asked Aug 17 '11 20:08

bugtussle


People also ask

How do you find the upper bound of an array?

For upper_bound(): Initialise the startIndex as 0 and endIndex as N – 1. Compare K with the middle element(say arr[mid]) of the array. If the middle element is less than equals to K then update the startIndex as middle index + 1(mid + 1).

What is UBound in VB6?

The UBound function is used with the LBound function to determine the size of an array. Use the LBound function to find the lower limit of an array dimension. UBound returns the following values for an array with these dimensions: Statement. Return Value.

How do you declare an array in VB6?

Declaring arraysArrays may be declared as Public (in a code module), module or local. Module arrays are declared in the general declarations using keyword Dim or Private. Local arrays are declared in a procedure using Dim or Static. Array must be declared explicitly with keyword "As".

What is UBound in VB net?

The UBound function returns the largest subscript for the indicated dimension of an array. Tip: Use the UBound function with the LBound function to determine the size of an array.


2 Answers

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)

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.

like image 195
Hans Olsson Avatar answered Sep 29 '22 09:09

Hans Olsson


You could write your own split function to do this:

Private Sub SplitLongs(ByVal strData As String, ByRef lng() As Long)
    Dim i As Integer
    Dim s() As String
    s = Split(strData, ",")
    If UBound(s) = -1 Then
        ReDim lng(-1 To -1)
    Else
        ReDim lng(LBound(s) To UBound(s))
        For i = LBound(s) To UBound(s)
            If IsNumeric(s(i)) Then lng(i) = s(i)
        Next
    End If
End Sub
like image 41
Matt Wilko Avatar answered Sep 29 '22 11:09

Matt Wilko