Test an array for empty?  | |
March 20th, 2002, 09:55 AM
|
#1 (permalink)
| | ph34r t3h g04t
Join Date: Oct 2001 Location: Kingsford, MI
Posts: 19,579
|
How do you test an array to see if it's empty? Basically, here's what I got:
VB6! Make sure you look at both functions or it probably won't make all that good a sense. Anyway, I need to check to see if aFiles has any data. It errors in the For Each loop if it doesn't. What? I could use For i = 0 to UBound(aFiles)?? I know, I just want to do it this way. And knowing how to test an unset array would be nice too... Code: Public Sub DirectoryMain()
Dim Directory As New DirChooser
Dim sLocation As String
Dim fMaster As String
Dim fCopy As String
Dim aFiles() As String
Dim sFile As Variant
On Error GoTo ErrorHandler
sLocation = Directory.GetFolderName
fMaster = "Q:\cad\acad\validation-tool\qc2k.mdb"
fCopy = sLocation & "qc2k.mdb"
FileCopy fMaster, fCopy
aFiles = MakeDrawingArray(sLocation)
' TriQuickSortString is an array sorting function not shown here.
TriQuickSortString aFiles
' Now here is where I would like to test aFiles
' to see if it's been given any data by assigning
' it to the MakeDrawingArray function...
For Each sFile In aFiles
If UCase(Right(sTemp2, 8)) <> "TITL.DWG" _
And UCase(Right(sTemp2, 12)) <> "TBLKINFO.DWG" Then
ThisDrawing.Application.Documents.Open sLocation & sFile
End If
Next
Exit Sub
ErrorHandler:
Resume Next
End Sub
Public Function MakeDrawingArray(ByVal sLocation As String)
Dim aFiles() As String
Dim sFile As String
Dim z As Integer
sFile = Dir(sLocation & "*.dwg")
ReDim aFiles(0) As String
aFiles(0) = sFile
sFile = Dir
z = 1
While sFile <> ""
ReDim Preserve aFiles(z) As String
aFiles(z) = sFile
sFile = Dir
z = z + 1
Wend
MakeDrawingArray = aFiles
End Function
I think that should do it... Thanks.
-Whir
Last edited by Whir : March 20th, 2002 at 10:00 AM.
|
| |
March 20th, 2002, 03:43 PM
|
#2 (permalink)
| | Senior Member
Join Date: Oct 2001
Posts: 881
|
you could check to see if the size of the array is 0...
or, you could check to see if each value in the array is 'null' or something like that with a while loop... if a value != 'null', then break from the loop and return that it's not empty.
-Z |
| |
March 21st, 2002, 01:40 PM
|
#3 (permalink)
| | ph34r t3h g04t
Join Date: Oct 2001 Location: Kingsford, MI
Posts: 19,579
|
If you dim an array as you have to to do what I'm doing 'Dim aFiles() as String' it doesn't support array methods. Therefor, no aFiles.Count. UBound(aFiles) similarly doesn't work if the there are no values in the array, and a For Each temp in aFiles would error because there are no 'temp's in aFiles when it's empty. aFiles(0) will also error because there's nothing there to check.
I thought there was a way to do something like If aFiles Is Empty or something like that, but that only works for a certain type of array and it's not the one I'm using. :P
-Whir |
| |
March 22nd, 2002, 12:45 AM
|
#4 (permalink)
| | Not Really a Member
Join Date: Oct 2001
Posts: 25,402
|
jeez everybody is doing things the hard way lol
If Not IsEmpty(aArrayName) Then
Set aArrayName = Nothing
End  lol
arghh I rmember that lol....
sorry misread 
Have you tried IsNull(aArray) or if aarray Is nothing ?
__________________
Helicopters don't fly; they vibrate so much and make so much noise that the earth rejects them.
|
| |
March 25th, 2002, 05:32 PM
|
#5 (permalink)
| | ph34r t3h g04t
Join Date: Oct 2001 Location: Kingsford, MI
Posts: 19,579
|
Ya, I've tried If Not Is Empty, Is Null, Is Nothing... Silly me, I was using a For To statement to ReDim the array. Hey dummy, is your integer 0? No? Then there are values in the array huh? Sometimes I'd like to shoot my fingers for not thinking of this stuff.
Also, you can make a seperate function that uses an error handler, and check for a value in Array(0). If it doesn't error it returns false, if it errors, it hits the error handler and returns true. Found that on Planet Source Code by a guy name Citizen Suicide. Kind of a hack way to do it, but it does work.
After figuring out (FINALLY) how to declare a control's connection to a database after initialization (thanks to MSDN for once) I finally got the app done. It's only been what, four months?
-Whir |
| |
March 25th, 2002, 06:02 PM
|
#6 (permalink)
| | Not Really a Member
Join Date: Oct 2001
Posts: 25,402
| Quote: |
After figuring out (FINALLY) how to declare a control's connection to a database after initialization (thanks to MSDN for once) I finally got the app done.
| That's why I just do it all in code, no ado controls.
Just put in the correct references and off you go 
Requires more code, but far more dynamic.
LOL check to see if element 0 has a value???
Ok now I've done some half a55 before but that's THIN LOL
Do you assign it a default value just in case it really isn't supposed to have a value?
Like if it doesn't have a value put in 0 so its more reliable to test? |
| |
March 26th, 2002, 01:35 PM
|
#7 (permalink)
| | ph34r t3h g04t
Join Date: Oct 2001 Location: Kingsford, MI
Posts: 19,579
|
No, see, that was another guy's method I found on PSC. I wouldn't do that because it's really hack and I'm not (ya right).
The problem I have with using code for connections is that I have to bind controls to the connection and I have NO idea how to do it. MSDN doesn't give me anything on it, and the guy that used to teach me stuff here doesn't have time anymore. If you have an ADO, or DAO in this case, control you can just bind text boxes to the control and voila, you're done.
To sovle the array problem, I just made my array builder assign Array(0) as False if it doesn't get redimed at all. Then I just test for that. It doesn't make sense, but if you saw the way I built the modules, you'd understand and probably show me an easier way to do it. :P
-Whir |
| |
March 27th, 2002, 12:30 AM
|
#8 (permalink)
| | Not Really a Member
Join Date: Oct 2001
Posts: 25,402
|
ahh binding...
I've never really had the oppurtunity to bind...
I always had variable table names, and many times column names so couldn't bind anything through the controls.
arghhh that really sucked making a stored procedure in T-SQL with variable table names
mostly I updated the controls on an event... whether it be changing rows in the recordset or whatever.
-- edit --
Cool, my 2400th post |
| |
March 27th, 2002, 11:00 AM
|
#9 (permalink)
| | ph34r t3h g04t
Join Date: Oct 2001 Location: Kingsford, MI
Posts: 19,579
|
Ya, that was the other option, which isn't bad, or hard, just a lot more code. :P Call me a wuss, but I'd rather let the controls take care of it, since that's what they're there for. :P
-Whir |
| | | Thread Tools | Search this Thread | | | | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | | | Most Active Discussions | | | | | Recent Discussions  | | | | | |