Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Empty array question 2

Status
Not open for further replies.

Transcend

Programmer
Sep 29, 2002
858
AU
Hi everyone

I've written a function that will return an array of strings.

My code is like so:

Dim sList() As String

sList = ReturnStrings()

However if no strings are returned i'm left with an empty array. I did a search on here to test for an empty array, but the only solution I found was to do an On Error Resume Next, and then test for the UBound of the array.

Is there a better way to do this?

Transcend
[goregous]
 
That is the only official way but there is an unofficial way in VB6 to create an array of strings with Ubound(-1). I discovered it accidentally...Split an empty string with a delimiter.

Dim strs() as string
strs = Split("",";")
Debug.Print UBound(strs) ' Should print -1
This will not work when upgraded to VB.Net but then in VB.Net you can allocate or Redim an array with -1.
Dim strs(-1) as String

Forms/Controls Resizing/Tabbing
Compare Code
Generate Sort Class in VB
Check To MS)
 
I last posted a solution to this some time ago (fx:keyword search...here we are...thread222-443102). Below is the refined version of that code (whether it is 'better' than throwing an exception is debatable, however). Pop the following in a module:
Code:
Option Explicit

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (lpDest As Any, lpSource As Any, ByVal cBytes As Long)
Public Declare Function VarPtrArray Lib "msvbvm60.DLL" Alias "VarPtr" (Ptr() As Any) As Long

Public Function ArrayHasElements(arrTest As Long) As Boolean
    Dim lResult As Long
    CopyMemory lResult, ByVal arrTest, 4 ' This is evil code
    ArrayHasElements = lResult
End Function
It must then be called similar to the following. Note that the VarPtrArray bit is important...:
Code:
Result=ArrayHasElements(VarPtrArray(yourarray))
 
JohnYingling:

Thanks, that's very helpful and very simple...I was recently using STR arrays in a program and was wondering why I was sometimes getting a "-1" for the UBound.

I'll be using that method from now on; previously I've been using something similar to Transcend:
Code:
Public Function ArrayHasElements(astr() As String) As Boolean
  On Error Resume Next
  ArrayHasElements = UBound(astr) + 1
End Function
 
Thanks guys

I'm not really sure which technique i like the least, but at least i know though there isn't a particularly recommended way to do this nicely.

Transcend
[gorgeous]
 
Could you instead Dim sSplit as a Varaint then you could do this...
Code:
Dim sSplit As Variant

Debug.Print IsArray(sSplit) 'Will return false

sSplit = ReturnStrings()

Debug.Print IsArray(sSplit) 'Will return True or False depending.

Casper

There is room for all of gods creatures, "Right Beside the Mashed Potatoes".
 
I tend to avoid variants whereever possible. Some people say that strongly typed code is restrictive, but experience has taught me otherwise...

 
I'm just thinking what is more expensive memory and risk wise. Variants or calling the CopyMemory API?

Casper

There is room for all of gods creatures, "Right Beside the Mashed Potatoes".
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top