?
Solved

Create empty VB integer array?

Posted on 2000-02-27
11
Medium Priority
?
1,034 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
11 Comments
 
LVL 7

Expert Comment

by:Vbmaster
ID: 2563647
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
ID: 2563650
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
ID: 2564341
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:sklein
ID: 2567157
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
ID: 2567295
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
 
LVL 13

Expert Comment

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

Author Comment

by:sklein
ID: 2570271
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
ID: 2570496
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 400 total points
ID: 2571368
How about using code like...

  ReDim ArrayName(-1 To -1)

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

Author Comment

by:sklein
ID: 2582044
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
ID: 2582071
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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 Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses

800 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