|
-
Mar 24th, 2004, 11:50 AM
#1
Thread Starter
PowerPoster
[resolved for the moment] IsEmptyArray()?
How can I get this to work?
VB Code:
Public Sub Foo(List() As Long)
If IsEmpty(List) Then
MsgBox "List is empty"
Else
MsgBox "List contains " & UBound(List) + 1 & " elements."
End If
End Sub
Private Sub Form_Load()
Dim Temp() As Long
'This would work but I need to check for EMPTY arrays
'ReDim Temp(2)
Foo Temp
End Sub
Basically I need a function to check if a array contains any elements or not.
Last edited by Fox; Mar 24th, 2004 at 02:13 PM.
-
Mar 24th, 2004, 11:53 AM
#2
How about this...
VB Code:
Private Function IsArrayEmpty(SourceArray As Variant) As Boolean
On Error Resume Next
Dim l As Long
l = UBound(SourceArray) - LBound(SourceArray) + 1
IsArrayEmpty = (l > 0)
End Function
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Mar 24th, 2004, 11:57 AM
#3
Thread Starter
PowerPoster
Nah calling another function out of my function would result in too much performance loss. Besides, this would be shorter:
VB Code:
Public Function IsArray(List() As Long) As Boolean
On Error Resume Next
IsArray = UBound(List) > -1
End Function
But that's not what I'm looking for.
-
Mar 24th, 2004, 12:04 PM
#4
Originally posted by Fox
But that's not what I'm looking for.
How could it do exactly what you asked, and yet not be what you are looking for? What is it you are looking for then?
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Mar 24th, 2004, 12:10 PM
#5
I'll offer some potentially useless advice here, but it's something I'm good at, so here it goes:
VB Code:
Private Sub cmdCheckArray_Click()
Dim check As Boolean
Dim number As Integer
check = False
For number = 1 To Len(Array())
If Array(number) = "" Then
check = True
End If
Next number
If Empty = True Then
MsgBox "Array Is Empty."
Else
MsgBox "Array contains " & Len(Array()) & " elements."
End If
End Sub
I'm not in VB right now, so I'm not sure if it's the exact syntax, but it should be close.
-
Mar 24th, 2004, 12:27 PM
#6
Thread Starter
PowerPoster
crptcblade: I'm looking for a built-in function to check whether a array contains any elements or not.
timeshifter: This won't work at all, you can't use Len() on arrays.
-
Mar 24th, 2004, 01:13 PM
#7
Frenzied Member
you're going to have to use On Error, or look at where the array is being defined/filled
-
Mar 24th, 2004, 01:37 PM
#8
Fanatic Member
One approach is to get the SAFEARRAY header and check the cDims member.
-
Mar 24th, 2004, 01:38 PM
#9
VB Code:
Public Sub Foo(List() As Long)
On Error Resume Next
MsgBox "List contains " & UBound(List) + 1 & " elements."
If Err <> 0 Then
MsgBox "List is empty"
End If
End Sub
-
Mar 24th, 2004, 01:51 PM
#10
Originally posted by Fox
crptcblade: I'm looking for a built-in function to check whether a array contains any elements or not.
timeshifter: This won't work at all, you can't use Len() on arrays.
Hrmmm.... UBound is built-in.... LBound is built-in.... WHAT's the problem?!?!?!?!!?
TG
-
Mar 24th, 2004, 01:57 PM
#11
Originally posted by techgnome
Hrmmm.... UBound is built-in.... LBound is built-in.... WHAT's the problem?!?!?!?!!?
TG
If you just do
VB Code:
Dim Temp() As Long
Msgbox UBound(Temp)
you get an error.
Fox: There is no built in way.
-
Mar 24th, 2004, 02:13 PM
#12
Thread Starter
PowerPoster
Okay thanks for the input. I'll try azteched's idea and compare the performance to a simple error catching.
*bows*
-
Mar 24th, 2004, 03:35 PM
#13
Addicted Member
I may be wrong
but for me foo is a "fonction orientée objet" so why not include variable in your object that keep track of the number of objects in your lists? Something like java does for the collections.
(An object that keep track of the list object)
When you add something to the list, add one to your variable
and when you withdraw something, substract one from it.
When you need to know how much object is in your list, you have only a variable tchek, which is something really fast.
In other word, is a sort of a wrapper for your lists with some methods like add, remove and sizeOf
-
Mar 24th, 2004, 04:06 PM
#14
Hyperactive Member
VB Code:
Private Declare Function SafeArrayGetDim Lib "oleaut32.dll" (ByRef saArray() As Any) As Long
and
VB Code:
Dim ElementCount as Long
Dim aArray () as Long
ElementCount = SafeArrayGetDim(aArray)
returns zero if array is empty otherwise returns non-zero.
-
Mar 24th, 2004, 04:40 PM
#15
Fanatic Member
yep, that does the cDims checking for you.
-
Mar 24th, 2004, 04:50 PM
#16
Frenzied Member
if you're that worried about speed surely you'll want the On Error approach vs the api call
-
Mar 24th, 2004, 05:09 PM
#17
In any case unless you are going to be doing hundreds (thousands?) of checks in a row, you won't see any difference.
-
Mar 24th, 2004, 05:49 PM
#18
Fanatic Member
The api call may well be faster, because internally VB will check the dimensions when you call UBound anyway, that's where you get the error raised from. VB has to set the error number too. Either way, it's going to be a completely negligible difference as ML said.
-
Mar 24th, 2004, 08:00 PM
#19
Thread Starter
PowerPoster
Okay everyone, I made the speed test and here's the results. The only notable things were that the API is getting slower as the array size grows while the OnError method isn't affected by that. Instead OnError gives bad results if the array is empty.
Code:
// No Redim
On Error: 7.98
SafeArray: 0.16
// Error Resume Next
On Error: 8.12
SafeArray: 0.15
// Redim Temp(0)
On Error: 0.52
SafeArray: 2.74
// Error Resume Next
On Error: 0.51
SafeArray: 2.73
// Redim Temp(200)
On Error: 0.51
SafeArray: 19.19
// Error Resume Next
On Error: 0.53
SafeArray: 17.18
As you can see I made 3 test with different array sizes. The test looks like this:
Code:
'Test functions
Public Function CheckArray_SafeArray(iArray() As String) As Boolean
CheckArray_SafeArray = (SafeArrayGetDim(iArray) > 0)
End Function
Public Function CheckArray_OnError(List() As String) As Boolean
On Error Resume Next
CheckArray_OnError = UBound(List) > -1
End Function
'Caller sub
Public Sub Main
For A = 0 To Count: Temp = CheckArray_OnError(Foo): Next
For A = 0 To Count: Temp = CheckArray_SafeArray(Foo): Next
On Error Resume Next
For A = 0 To Count: Temp = CheckArray_OnError(Foo): Next
For A = 0 To Count: Temp = CheckArray_SafeArray(Foo): Next
End Sub
As you can see the 2nd calls are just with activated On Error in the caller sub - which is speeding up the code a very little.
Last edited by Fox; Mar 24th, 2004 at 08:07 PM.
-
Mar 24th, 2004, 08:12 PM
#20
Fanatic Member
Strange.. I wouldn't have thought either method would vary in speed with size of array, especially the API, as I presume the SAGD function is a thin wrapper something like:
Code:
int SafeArrayGetDim(SAFEARRAY*s){
return s->cDims;
}
-
Mar 24th, 2004, 10:48 PM
#21
Thread Starter
PowerPoster
There is probably some overhead for each data item when calling external DLL functions.. Couldn't expain this otherwise.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|