Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Create empty VB integer array?

Posted on 2000-02-27
11
Medium Priority
?
1,047 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
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.

 

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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

618 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