Solved

Create empty VB integer array?

Posted on 2000-02-27
11
962 Views
Last Modified: 2008-03-17
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
Comment
Question by:sklein
  • 5
  • 4
  • 2
11 Comments
 
LVL 7

Expert Comment

by:Vbmaster
Comment Utility
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
 
LVL 7

Expert Comment

by:Vbmaster
Comment Utility
Two type errors - the last function should contain the code...

    'Add a item
    Filter = 1
    ReDim IntegerArray(0)   'changed
    IntegerArray(0) = 10     'changed
0
 
LVL 13

Expert Comment

by:crazyman
Comment Utility
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
 

Author Comment

by:sklein
Comment Utility
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
 
LVL 7

Expert Comment

by:Vbmaster
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 13

Expert Comment

by:crazyman
Comment Utility
Lol
 i was just being picky because it was a sub but had
end function
at the end...
0
 

Author Comment

by:sklein
Comment Utility
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
 

Author Comment

by:sklein
Comment Utility
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
 
LVL 7

Accepted Solution

by:
Vbmaster earned 100 total points
Comment Utility
How about using code like...

  ReDim ArrayName(-1 To -1)

This will make the UBound(ArrayName) = -1.
0
 

Author Comment

by:sklein
Comment Utility
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
 

Author Comment

by:sklein
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now