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!

How do I test if a dynamic array has elements? 2

Status
Not open for further replies.

tedsmith

Programmer
Nov 23, 2000
1,762
AU
First I dimension an array
Public MyArray() as string

Then I feed it with received data buffer of items separated by VbCr thus -
MyArray=Split(Buffer,VbCr)

I can test if data was received OK later by Ubound(MyArray) giving the number of elements or testing the first element.

But my problem is if the data is not received, the Array still has no elements. Ubound(MyArray) and any attempt to test a MyArray element gives an error.
IsEmpty(MyArray) gives false whether the data is there or not.

Apart for using a separate On Error Goto or having an additional flag, is there any other way of directly testing if a dynamic array is empty or not?
 
Thanks for the advice on threads.
I was hoping someone had come up with something else since.

Thanks strongm for your old posting using APIs

It would seem that using an On Error seems to be the lesser of a few evils unless someone can advise otherwise.
 

If I understand your requirements right, then for your single dimensioned string arrays you could use this:

bIsEmpty = CBool(LenB(Join(MyArray, vbCr)) = 0)

But be aware that your buffer could contain empty elements and have only the delimiters such as:
Buffer = vbCr & vbCr & vbCr
In this case you would also need to ignore the delimiters when testing:

bIsEmpty =CBool(LenB(Replace(Join(MyArray, vbCr), vbCr, vbNullString)) = 0)
 
Now why didn't I think of joining it first!

I don't ever have only vbCrs so all I need is
If Len(Join(MyArray,vbCr)) > 0 then (Array has some data)

This is much more simple than the examples in the threads referred to.

Have a star!
 
The examples in the threads are generic, in that they will work no matter what the contents of the array. SBerthold's solution works for strings only - which, of course, is ideal for you.

Mind you, I realise now that I am confused. Splitting an empty string should give you a Ubound of -1 for the resulting array, not an error.
 
Yes it is only good for strings.
Any attempt to use Ubound on a dynamic array that has not yet had data fed to it, gives an error (as per the previous threads quoted).
However if you make the dynamic array ="" then its Ubound = -1
Thats why someone suggested making it "" if there was no data to split.

Ubound gives -1 if the the array has been static dimensioned and not filled with data but not for a dynamic dimensioned array.

If after being filled, you erase it and redim, the same error occurs again.




 
My point is that your:

MyArray=Split(Buffer,VbCr)

should result in a Ubound of -1 for MyArray if Buffer is empty.




 
Yes, I was confused by that too. I put a text box (setting MultiLine to true) and command button on a form, and did this:
Code:
Option Explicit
Dim a1() As String

Private Sub Command1_Click()
a1 = Split(Text1.Text, vbCr)
MsgBox UBound(a1)
End Sub

This works as strongm suggests, such that ubound is -1 if the text box is empty.

On the other hand, I get an error message (subscript out of range) as expected if I attempt to evaluate ubound of the array before setting it equal to the result of the split function.

So, perhaps there is a wealth of hidden meaning in the phrase "data is not received.
 

My Split expression is in a Sub DataArrival(..)

The Ubound(MyArray) is in another Sub triggered by a timer to look at the data when it wants it.

If the data never gets received then MyArray is never presented with the Split expression so it is empty and you get a the subscript error if you test it later to see if something has been received!

Currently I get around this by having another variable flag that is set to true when some data is received and the test is only then done if this flag is true. It is set to false after the data has been used.

The reason I asked originally this question was whether there was a direct way so I didn't have to manage this extra flag.(Like IsEmpty which also doesn't work here either)
 
Fine. That explains much better than your original post, which describes a somewhat different logic flow ...

Now, let's think outside the box ...


Why not at the point where you originally dim (and presumably at some point clear) the dynamic string array immediately call something like

MyArray=Split("",VbCr)

MyArray now has a Ubound of -1. So you can now safely test Ubound later in your program safe in the knowledge that you won't get an error and a result of -1 will indicate that no data has been received.



 
Thanks
Yes I think that method was covered in previous threads.
SBerthold's Join method ( If Len(Join(MyArray,vbCr)) > 1 then) doesn't need any action when clearing.

I don't need to worry if it only contains only vbCrs because the further processing takes care of that.
 
Just the maximum 8192 bytes allowed by Winsock in any one firing of the Dataarrival.
 
Yes, I suspected this was related to that.

You might want to do some timings ...
 
This statement only has to be done once all the data is collected so it shouldn't be a problem
 
janebush08, I can't see what the link you included has to do with this question. It refers to NET and the WEB, both of which have nothing to do with my question.
 
> can't see what the link you included has to do with this question

if you read the profile for this user it will become clear
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top