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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Determining if an array has been dimmed 2

Status
Not open for further replies.

yumbelie

Programmer
Dec 31, 2002
96
GB
Hi,

Is there a function by which you can determine if an array has been dimmed? Basically, I dim a global array as:

Dim MyArray() As String

Now in a function, if a certain string contains stuff, the contents is Split() into the array. Problem is, if I have nothing in the string, then the Split never takes place, and functions that call on the array later on encounter Error 9: Subscript out of range errors. Is there a way I can test for this condition? (And therefore declare the empty array to an arbritrary size) ?

Thanks

Yum.
 
Check the ubound and lbound functions

these return the upper and lower limits of the array.

Code:
dim myArr() as string

myArr = split("This,is,a,test,and,what,a,good,test,it,is,too",",")

debug.print ubound(myArr)
debug.print lbound(myArr)


Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
Hi, thanks for the reply. Problem is that won't solve the issue unfortunatly. As the array is undeclared (contains no elements) those functions will both return Error 9: Subscript out of range. I need to know if the array has been declared with any elements, so basically if the Split() function has been invoked.
 
Sorry, I understand now!

the following code works for me

Code:
Dim myArr1() As String, myArr2() As String
Dim strTest1 As String, strTest2 As String

ReDim myArr1(0)
ReDim myArr2(0)


'strTest1 = "This,is,a,test,and,what,a,good,'test,it,is,too"
'strTest2 = ""
'myArr1 = Split(strTest1, ",")
'myArr2 = Split(strTest2, ",")

Debug.Print "UBound 1 is "; UBound(myArr1)
Debug.Print "LBound 1 is "; LBound(myArr1)
Debug.Print "UBound 2 is "; UBound(myArr2)
Debug.Print "LBound 2 is "; LBound(myArr2)

The code above executes either with or without the split functions commented and presents both LBound and UBound equal to zero.

With the split functions uncommented we get a UBound of 11 for the first string and -1 for the empty one!


Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
See my code in thread222-836312, but read the comments as to whether it is actually a better solution than simply handling the error
 
Many thanks, that is *exactly* what I wanted. Bit of a mess really - you'd think someone at MS would have thought about that when allowing you to create empty arrays :\
 
Can you accomplish the same thing with ....

if myArr1() = "" then
msgbox "The array is empty"
else
msgbox "The array is split"
end if
?
 
correction ...

Can you accomplish the same thing with ....

if myArr1(0) = "" then
msgbox "The array is empty"
else
msgbox "The array is split"
end if
?
 
Sadly no. If the array is genuinely empty (no elements dimensioned yet) then

if myArr1(0) = "" then

will generate an Error 9: Subscript out of range, which is what the orginal poster wants to avoid
 
How about ...

if myArr1() = "" then
msgbox "The array is empty"
else
msgbox "The array is split"
end if
Will that work Strongm?
 
Writtten for a String array, but that shouldn't matter.
Code:
Function IsArrayDimmed (Arr() as String)as Boolean
  Dim UB as long
  Dim LB as long
  On Error GoTo IsArrayDimmedErr
    UB=UBound(Arr)
    LB=LBound(Arr)
  On Error GoTo 0

IsArrayDimmedExit:
  Exit Function

IsArrayDimmedErr:
  IsArrayDimmed=False
  Resume IsArrayDimmedExit

End Function

Pat O'Connell
 
Thanks for the contribution, another technique :) - Is error handling not a rather 'tacky' way to deal with an undimensioned array? - I'm not passing judgement or anything, but from my previous c experience, error handling was for handling errors - I know VB flags it as an error, but it doesn't really seem like a true error - e.g. like when you try to write to an array element greater then the arrays nth dimension. Oh well, thanks anyway:)
 
yumbelie, you said:

"Thanks for the contribution, another technique :) - Is error handling not a rather 'tacky' way to deal with an undimensioned array? - I'm not passing judgement or anything, but from my previous c experience, error handling was for handling errors..."

True enough, but the only way of determining the bounds of an array will raise an error if the array is not dimensioned. So trap for the error in the function and handle it elsewhere.

As you state, if your program has nothing to parse, that needs to be detected somehow.

Another way (other than the above function) is to determine if the string that would be split is empty before trying to split it, then skip processing that line if it's empty.

Pat O'Connell
 
i have used this and it seems to work fine:

Code:
If Len(Join(myArray, "")) = 0 Then
   'do whatever
End If

i don't remember where i got it or i'd give someone some credit...
 
bbolte, that's going to 'do whatever even if the the array has been dimmed

Dim myArray(3) As String
If Len(Join(myArray, "")) = 0 Then
'do whatever"
End If

I recommend taking a look at the thread recommended by strongm above.

"Two strings walk into a bar. The first string says to the bartender: 'Bartender, I'll have a beer. u.5n$x5t?*&4ru!2[sACC~ErJ'. The second string says: 'Pardon my friend, he isn't NULL terminated'."
 
it seemed to me that he was more interested in whether the array had elements rather than simply being dimmed. if you do this:

Code:
Dim myArray()

Private Sub Form_Load()
    
    If Len(Join(myArray, "")) = 0 Then
       MsgBox "Array is empty"
    Else
        MsgBox "Array has elements"
    End If
End Sub

it shows the "Array is empty" message box. do this:

Code:
Dim myArray()

Private Sub Form_Load()
    ReDim myArray(5)
    myArray(0) = "Stuff"
    
    If Len(Join(myArray, "")) = 0 Then
       MsgBox "Array is empty"
    Else
        MsgBox "Array has elements"
    End If
End Sub
and it shows the "Array has elements" message box. that's all i was trying to do with my initial code segment.
 
But if you do this,
Code:
Dim myArray()

Private Sub Form_Load()
    ReDim myArray(5)
        
    If Len(Join(myArray, "")) = 0 Then
       MsgBox "Array is empty"
    Else
        MsgBox "Array has elements"
    End If
End Sub
it returns "Array is empty", which is true but the array also has elements. So the second message box should also be shown.

Seems as though your conditions are comparing apples and oranges. I think that in this thread we are trying to avoid error 9 (subscript out of range) which is about array elements and not whether the array holds any data.

We need to determine if an array has elements or does not have elements. strongm's code does that.


zemp
 
>it seemed to me that he was more interested in whether the array had elements rather than simply being dimmed


The requirement to deal with "Error 9: Subscript out of range errors" would suggest otherwise. An quick analysis of the question would suggest that yumbelie is saying, if the source string is empty, no code to do the split is run, rather than the alternative which is that if the string is empty, the split returns nothing, as the latter is patently untrue; if you try the latter you will find that Ubound and LBound work fine, returning -1 and 0).

i.e the difference between
Code:
Dim strResult() As String
Dim strSource As String
strSource = ""
If Len(strSource) <> 0 Then
    strResult = Split(strSource, "splitmehere")
Else
    ' Do not do a split because source string is empty
End If
and
Code:
Dim strResult() As String
Dim strSource As String
strSource = ""
strResult = Split(strSource, "splitmehere")


Your code is unable to discriminate the different result from the If and the Else in the first example. It can determine whether the second example returned any data or not - but then so can a simple Ubound

There is an argument, given this view, that perhaps yumbelie would be better off doing the split whether or not the source string has anything in it or not, as this makes subsequent handling a little easier
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top