Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1054
  • Last Modified:

Create empty VB integer array?

Split("") returns an "empty" string array (ubound = -1). Anyone noticed an equivalent function that returns an empty integer array? Alternatively, has anyone found a way to set an integer array to empty?

I'm trying to create a filter function that takes in a string array and returns an integer array, but since an empty string array is a valid input, I need to be able to create an equivalent empty integer array for return.
0
sklein
Asked:
sklein
  • 5
  • 4
  • 2
1 Solution
 
VbmasterCommented:
There is two methods to do this. For both of these you use a dynamic array but I guess you already know how to use these. I'll show these in two examples. The first one...

  Sub Filter(String, IntegerArray() As Integer)

    'Start with no items
    ReDim IntegerArray(0)
    Filter = 0

    'Add a item
    ReDim IntegerArray(1)
    IntegerArray(1) = 10

  End Function


  Dim a As Integer
  Dim sValue As String
  Dim IntegerArray() As Integer

  Call Filter(sValue, IntegerArray())

  If (UBound(IntegerArray) = 0) Then
    'No items was saved into the array
  Else
    'Do something with the saved item (from 1..Ubound(IntegerArray))
    For a = 1 to Ubound(IntegerArray)
    Next
  End If


You can also use a function to return the number of items saved into the array. The advantage of this method is that it is more efficient than the first one, this new method uses all items.

  Function Filter(String, IntegerArray() As Integer) As Integer

    'Start with no items
    Erase IntegerArray     'this line will clear the array
    Filter = 0

    'Add a item
    Filter = 1
    ReDim IntegerArray(1)
    IntegerArray(1) = 10

  End Function


  Dim a As Integer
  Dim sValue As String
  Dim IntegerArray() As Integer
  Dim Count as Integer

  Count = Filter(sValue, IntegerArray())

  If (Count = 0) Then
    'No items was saved into the array
  Else
    'Do something with the saved item (from 0..Count-1)
    For a = 0 To (Count-1)
    Next
  End If
0
 
VbmasterCommented:
Two type errors - the last function should contain the code...

    'Add a item
    Filter = 1
    ReDim IntegerArray(0)   'changed
    IntegerArray(0) = 10     'changed
0
 
crazymanCommented:
Another error ...
Function Filter(String, IntegerArray() As Integer)

                               'Start with no items
                               ReDim IntegerArray(0)
                               Filter = 0

                               'Add a item
                               ReDim IntegerArray(1)
                               IntegerArray(1) = 10

                             End Function





The first function shouldnt be a sub...:o)
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
skleinAuthor Commented:
Actually (as originally posted), I'm trying to produce a "filter function" that looks like the following:

Function CStrToInt(sArray() as String) as Integer()

In the case where sArray is empty (ubound(sArray) = -1), I want to return an Integer array which has the property that ubound() of it is also -1.

ubound(split("")) = -1, but split() returns a string array. I haven't been able to find an equivalent function that returns an "empty" integer array.

By the way, empty and uninitialized are different. ubound() of an uninitialized array generates an array bounds error.



0
 
VbmasterCommented:
Crazyman: actually it was supposed to be a sub to show that it did not need a return value, perhaps I should have said something about that. ;)

Sklein: The only way a function can return a array is (as far as I know) using a variant array. I believe using the approach with the result array as the second parameter is more efficient than using variants (variants are slow). Variant arrays are also used with the VB6 Split() function, if this is what you are trying to copy. Anyway, here's if you really want to use a variant array...

   Function Split() As Variant

      Dim SplitTemp As Variant
 
      ReDim SplitTemp(1)
      SplitTemp(0) = 1
      SplitTemp(1) = 23
      Split = SplitTemp

      'If you want to try returning a 'empty result' uncomment
      'the next line below this one
      'Split = Null
 
   End Function

The ReDim statement can not be used directly on the function variable, this is why I added the SplitTemp variable here. To try out the function you can use code like this (see the difference when you uncomment the "Split = Null" line and when it's commented)...

   Dim Res As Variant
 
   Res = Split()
   If IsNull(Res) Then
      Me.Caption = "Empty result"
   Else
      Me.Caption = UBound(Res)
   End If
0
 
crazymanCommented:
Lol
 i was just being picky because it was a sub but had
end function
at the end...
0
 
skleinAuthor Commented:
Actually (as originally posted), I'm trying to produce a "filter function" that looks like the following:

Function CStrToInt(sArray() as String) as Integer()

In the case where sArray is empty (ubound(sArray) = -1), I want to return an Integer array which has the property that ubound() of it is also -1.

ubound(split("")) = -1, but split() returns a string array. I haven't been able to find an equivalent function that returns an "empty" integer array.

By the way, empty and uninitialized are different. ubound() of an uninitialized array generates an array bounds error.



0
 
skleinAuthor Commented:
Unfortunately, using variant arrays as the result type doesn't work, as you can't assign a variant array to anything but another variant array. Thus, this model doesn't work for doing (say) type conversion.

If worse comes to worse, I'll do something like:

Function CStrToInt(saInput() as String, iaResult() as Integer) as Integer

where the function result contains the number of elements in the result array.

That way, for the case where the input array is empty, I can leave iaResult uninitialized and return a value of zero for the function.

But still, what I REALLY want is to figure out how to produce an integer array whose ubound() is -1. I suppose it should be feasible to write a C++ function that creates such an array. Or perhaps use LSet between two UDTs...

0
 
VbmasterCommented:
How about using code like...

  ReDim ArrayName(-1 To -1)

This will make the UBound(ArrayName) = -1.
0
 
skleinAuthor Commented:
Unfortunately, using variant arrays as the result type doesn't work, as you can't assign a variant array to anything but another variant array. Thus, this model doesn't work for doing (say) type conversion.

If worse comes to worse, I'll do something like:

Function CStrToInt(saInput() as String, iaResult() as Integer) as Integer

where the function result contains the number of elements in the result array.

That way, for the case where the input array is empty, I can leave iaResult uninitialized and return a value of zero for the function.

But still, what I REALLY want is to figure out how to produce an integer array whose ubound() is -1. I suppose it should be feasible to write a C++ function that creates such an array. Or perhaps use LSet between two UDTs...

0
 
skleinAuthor Commented:
Vbmaster--

I'll have to say I didn't think of that. It's not exactly the same, as I can access the -1'th element of the array (whereas I can't access the -1'th element of the value returned by split("")), but it's good enough for my purposes... Thanks.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 5
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now