Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to Check Whether an Array Is Empty

Posted on 2009-04-10
4
Medium Priority
?
1,912 Views
Last Modified: 2013-12-25
In the code below, strFileList is a array of file names (strings).

I want to be sure the array is not empty before I run the File Delete portion of the code.  

Will the method below work for skipping the File Delete portion if there are no values in the array?  

Is there a better way to check to see if the array is empty.
Dim strFileList As String()
        Dim strFileName As String
 
        Dim intFileListItemCount As Integer = 0
 
        If Directory.Exists(strBackupPath) = True Then
 
            strFileList = Directory.GetFiles(strBackupPath, "*.bak")
 
            For Each strFileName In strFileList
                intFileListItemCount = intFileListItemCount + 1
            Next
 
            If intFileListItemCount > 0 Then
                For Each strFileName In strFileList
                    File.Delete(strFileName)
                Next
 
                Return True
            Else
                Return False
 
            End If
        Else 'Directory does not exist
            Return False
        End If

Open in new window

0
Comment
Question by:TimSledge
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 48

Accepted Solution

by:
jpaulino earned 400 total points
ID: 24120212
You can check the Length of the array that gives you the number of elements you have

        If strFileName.Length = 0 Then
            Stop ' array is empty
        End If

Open in new window

0
 
LVL 48

Assisted Solution

by:Wayne Taylor (webtubbs)
Wayne Taylor (webtubbs) earned 100 total points
ID: 24120256
I believe that should be...

    strFileList.Length = 0

Wayne
        Dim strFileList As String()
        Dim strFileName As String
  
        If Directory.Exists(strBackupPath) = True Then
             strFileList = Directory.GetFiles(strBackupPath, "*.bak") 
            If strFileList.Length > 0 Then
                For Each strFileName In strFileList
                    File.Delete(strFileName)
                Next 
                Return True
            Else
                Return False 
            End If
        Else 'Directory does not exist
            Return False
        End If

Open in new window

0
 
LVL 48

Expert Comment

by:jpaulino
ID: 24120262
>> I believe that should be...
It was a typo :)
0
 
LVL 4

Author Comment

by:TimSledge
ID: 24120812
Thanks for your help!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question