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

Ubound problem

Status
Not open for further replies.

zzfive03

Programmer
Joined
Jun 11, 2001
Messages
267
Hello;

I have an array which is dynamicly populated from the results of a SQL result set. I want to check to see if the array contains records. When I do a:
myArray_ubound = Ubound(myArray)
response.Write myArray_ubound

The Ubound is always 1 less than the total # of records (telling me that VB uses a 0 based array).

This means that if there is 1 record, the ubound = 0. However, my problem is that how can i tell if the array is empty? It appears it is still a result of 0, before I ever assign it. Therefore makeing a empty array, and an array with 1 value have a Ubound of 0.

Am I doing something wrong? Is there an easyer way to determien if there is a value in my array?

Thanks in advance.
 
For the reasons you have identified, do not use the Ubound as your mechanism for detecting presence/absence of values. Wherever/whenver you populate the array from the recordset, use...
Code:
option explicit
... fill recordset (rs) ...
dim bRecordsToProcess, aryStuffFromRS 
if rs.eof then
   bRecordsToProcess = false
else
   bRecordsToProcess = true
   aryStuffFromRS = rs.getrows()
end if

The use of getrows() assumes the only thing coming back in the recordset is the data you want. Even if that's not the case, setting the switch based on the recordset is still your best bet.

Hope this helps
 
The array bounds thing is because unlike just about every other language on the planet VB doesn't declare arrays based on the number of elements, but rather on the upper bound. This is commonly mis-taught and mis-quoted as MS attempting to make it easier for both the people who are used to starting with 0 and the people who are used to starting with 1.
In fact I actually took a VB class once (needed some easy credits) and the professor continued to misquote this concept.
Detecting whether an array has elements or not is always a difficult task. Mainly because there are so many ways that ought to work and don't.
As lottastuff said above, it would be better for you to check that you have data coming back than to check if you have something in the array, what is most likely happening is that the first element is getting set, but it is getting set to a null value. You could test this guess by checking if array(0) = "".
Unfortunatly if you attempt to use the isEmpty() function you will not get the results that the sane person would think you will.
Code:
Dim myArray(0), myArray2(0)
Response.Write isEmpty(myArray) & &quot;<br>&quot;
Response.Write isEmpty(myArray2) & &quot;<br>&quot;
Will write:
False
False

So instead what you will need to do is one of two things:
1) Assume if the UBound = 0 and isEmpty(myArray(0)) = True that the array is actually empty.
2) Loop throught the array checking all elements
[code]
Function isArrayEmpty(tArray)
   Dim i, hasContent
   hasContent = false
   For i = LBound(tArray) to UBound(tArray)
      hasContent = hasContent OR NOT isEmpty(tArray(i))
   Next
   isArrayEmpty = hasContent
End Function

Basically this will loop through your array checking each element to see if it is empty. It uses logical OR to update the status of the hasContent variable. If it is empty, then False get's OR'd, if it isn't empty than True gets OR'd.

If anyone has an even more efficient way, please let me know.

-Tarwn Experts are only people who have realized how much they will never know about a language.
________________________________________________________________________________
Want to get great answers to your Tek-Tips questions? Have a look at faq333-2924
 
The array bounds thing is because unlike just about every other language on the planet VB doesn't declare arrays based on the number of elements, but rather on the upper bound. This is commonly mis-taught and mis-quoted as MS attempting to make it easier for both the people who are used to starting with 0 and the people who are used to starting with 1.
In fact I actually took a VB class once (needed some easy credits) and the professor continued to misquote this concept.
Detecting whether an array has elements or not is always a difficult task. Mainly because there are so many ways that ought to work and don't.
As lottastuff said above, it would be better for you to check that you have data coming back than to check if you have something in the array, what is most likely happening is that the first element is getting set, but it is getting set to a null value. You could test this guess by checking if array(0) = &quot;&quot;.
Unfortunatly if you attempt to use the isEmpty() function you will not get the results that the sane person would think you will.
Code:
Dim myArray(0), myArray2(0)
Response.Write isEmpty(myArray) & &quot;<br>&quot;
Response.Write isEmpty(myArray2) & &quot;<br>&quot;
Will write:
False
False

So instead what you will need to do is one of two things:
1) Assume if the UBound = 0 and isEmpty(myArray(0)) = True that the array is actually empty.
2) Loop throught the array checking all elements
[code]
Function isArrayEmpty(tArray)
   Dim i, hasContent
   hasContent = false
   For i = LBound(tArray) to UBound(tArray)
      hasContent = hasContent OR NOT isEmpty(tArray(i))
   Next
   isArrayEmpty = hasContent
End Function

Basically this will loop through your array checking each element to see if it is empty. It uses logical OR to update the status of the hasContent variable. If it is empty, then False get's OR'd, if it isn't empty than True gets OR'd.

If anyone has an even more efficient way, please let me know.

-Tarwn Experts are only people who have realized how much they will never know about a language.
________________________________________________________________________________
Want to get great answers to your Tek-Tips questions? Have a look at faq333-2924
 
Thank you both for your help.

Mark
 
Sorry for the double post everyone, I know the last thing every one wanted for christmas this year was one of my posts duplicated :)

-Tarwn Experts are only people who have realized how much they will never know about a language.
________________________________________________________________________________
Want to get great answers to your Tek-Tips questions? Have a look at faq333-2924
 
If you just HAVE to only look at the array, then don't bother with writing your own IsArray function. VBS has had one since version 1:

Code:
<%option explicit
dim aStuff
if IsArray(aStuff) then 
	response.write &quot;<br>Step 1: VBS thinks it's an aray. (dimmed only)&quot;
else
	response.write &quot;<br>Step 1: not yet. (dimmed only)&quot;
end if

aStuff = array(&quot;One&quot;, &quot;Two&quot;, &quot;Three&quot;)
if IsArray(aStuff) then 
	response.write &quot;<br>Step 2: VBS thinks it's an aray. (array element filled)&quot;
else
	response.write &quot;<br>Step 2: not yet.  (array element filled)&quot;
end if

redim aStuff(0)
if IsArray(aStuff) then 
	response.write &quot;<br>Step 3: VBS thinks it's an aray. (redimmed to 0 ellements)&quot;
else
	response.write &quot;<br>Step 3: not yet. (redimmed to 0 ellements)&quot;
end if

set aStuff = nothing
if IsArray(aStuff) then 
	response.write &quot;<br>Step 4: VBS thinks it's an aray. (aStuff <i>object</i> set to nothing)&quot;
else
	response.write &quot;<br>Step 4: not an array. (aStuff <i>object</i> set to nothing)&quot;
end if

aStuff = array(&quot;One&quot;, &quot;Two&quot;, &quot;Three&quot;)
if IsArray(aStuff) then 
	response.write &quot;<br>Step 5: VBS thinks it's an aray. (array element re-filled)&quot;
else
	response.write &quot;<br>Step 5: not yet.  (array element re-filled)&quot;
end if
%>

Note the use of option explicit and step 5. VBS treats arrays as objects from the get-go. I'll wager that a call to the getrows method on the recordset (when the query returns no rows) will leave the target array in the same state as when it was declared (but not dimmed).
 
Nifty and all, but this doesn't help determine if there are valid records in the array, only that the object is an array. What we were attempting to discover is if there was valid data in the array, rather than an array of nulls. Your step 3 actually demonstrates this quite well. ASP tells us this is an array when we use isArray, it also tells us this object is not empty (it has a single reference, aStuff(0) ) and the the UBound is 0.
Code:
<%
option explicit
dim aStuff(0)

'aStuff(0) = &quot;hereIsAValue&quot;

if IsArray(aStuff) then 
    response.write &quot;It is an array<br>&quot;
else
    response.write &quot;It is not an array<br>&quot;
end if

If isEmpty(aStuff) then
	Response.Write &quot;It is empty<br>&quot;
Else
	Response.Write &quot;It is not empty<br>&quot;
End If

Response.Write &quot;The UBound is &quot; & Ubound(aStuff) & &quot; and there are &quot; & (UBound(aStuff) - LBound(aStuff) + 1 ) & &quot; elements in the array.<br>&quot;

If isEmpty(aStuff(0)) Then
	Response.Write &quot;No value assigned in element&quot;
Else
	Response.Write &quot;The value of the first element is: &quot;&quot;&quot; & aStuff(0) & &quot;&quot;&quot;<br>&quot;
End If
%>

Running it once with the assignment commented out, ie no value in the array, give us:
It is an array
It is not empty
The UBound is 0 and there are 1 elements in the array.
No value assigned in element


Running it after assign a value gives us:
It is an array
It is not empty
The UBound is 0 and there are 1 elements in the array.
The value of the first element is: &quot;hereIsAValue&quot;


In both cases the isArray and the isEmpty function did not help us to ascertain whether there was really any value in the elements of the array, neither did the UBound or LBound.

The reason for this is that there is memory allocated for the array and the memory contains a single memory reference for element 0. Therefore the array is not actually empty and it is still an array, but the array element has no value and is in fact null.

The original question was how to tell if the array elements were empty or had actual values, for which there is no built in function.

-Tarwn

BTW, I was aware of the isArray function. The basis of this function is the varType function, the numerical classification of the array variable type, and an if statement. Basically:
Code:
Function isArray(obj)
   If vartype(obj) = 8192 Then
      isArray = True
   Else
      isArray = False
   End If
End Function
Experts are only people who have realized how much they will never know about a language.
________________________________________________________________________________
Want to get great answers to your Tek-Tips questions? Have a look at faq333-2924
 
Sorry, one correction, didn't notice the missing &gt;
Code:
Function isArray(obj)
   If vartype(obj) >= 8192 Then
      isArray = True
   Else
      isArray = False
   End If
End Function
Experts are only people who have realized how much they will never know about a language.
________________________________________________________________________________
Want to get great answers to your Tek-Tips questions? Have a look at faq333-2924
 
Umm....&quot;I want to check to see if the array contains records.&quot;
 
I wasn't attempting to start anythinbg , only point out that verifying that it is an array did not, in my opinion, supply an answer to the question, which i actually believed was:
However, my problem is that how can i tell if the array is empty?


In both cases my interpretation was that they wanted to check if the array had valid values. An array is, by definition, never empty, as it has at least one indexed element. The question above seemed to be how to tell whether the indexed elements in an array are empty.

-Tarwn Experts are only people who have realized how much they will never know about a language.
________________________________________________________________________________
Want to get great answers to your Tek-Tips questions? Have a look at faq333-2924
 
Thanks again everyone, I wealth of info.

Mark.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top