Solved

Create empty VB integer array?

Posted on 2000-02-27
11
998 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
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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

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 100 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Add and format columns in vb6 7 63
VB6 - Scroll Mouse wheel on Picturebox 13 73
how to Classify of email (pdf attachments) 7 45
Dinamic report to Crosstab query 9 33
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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

828 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