【问题标题】:Array is empty, how to validate数组为空,如何验证
【发布时间】:2012-05-30 19:09:03
【问题描述】:

我正在开发一个程序,以便在文件老化一定时间后从某个文件夹中删除文件,并通过正则表达式或扩展名进行匹配。我遇到了一个问题,其中 files() 可能是

files(0) = Nothing 
files(1) = Nothing
files(2) = Nothing
ect.... 

现在它的写法,我可以放置

Else
                            log(1) = data(1)
                            log(3) = "Array field empty"
                            InsertLog(log)

并且程序将记录与file(i) = Nothing 一样多的文件。这将创建冗余的数据库记录,这是不希望的。有没有办法判断是否 ALL files(i) = Nothing 然后将代码插入到数据库中?

           'If log(3) is successful that means no files were old enough or deleted successfully
           If log(3) = "Success" And IsArray(files) Then 
                For Each file In files
                    If Not file.IsNullOrEmpty(file) Then
                        'If files is actually something insert into the log
                        log(1) = file
                        InsertLog(log)
                    'could place else here 
                    End If
                Next
                files = Nothing
            Else
                'If no files or error in directory perform this
                log(1) = data(1)
                InsertLog(log)
            End If

【问题讨论】:

标签: arrays vb.net ssis filesystems


【解决方案1】:

听起来你太复杂了。

  1. 创建文件时在数据库中创建时间戳
  2. 定期唤醒并检查是否需要删除任何内容
  3. 删除任何需要删除的内容

有时,当事情变得过于复杂时,也许是时候重新考虑您的攻击计划了。

【讨论】:

    【解决方案2】:
    Public Function AllArrayElementsAreNull(arr() As Object) As Boolean
        Dim FoundNonNullItem As Boolean = False
        For Each item As Object In Arr
            If item IsNot Nothing Then
                FoundNonNullItem = True
                Exit For
            End If
        Next
        Return Not FoundNonNullItem
    End Function 
    

    【讨论】:

      【解决方案3】:

      我添加了一个计数器来计算文件是否确实存在,简单的解决方案。

                 If log(3) = "Success" And IsArray(files) Then
                      j = 0
                      For Each file In files
                          If Not file.IsNullOrEmpty(file) Then
                              log(1) = file
                              InsertLog(log)
                              j += 1
                          End If
                      Next
                      If j = 0 Then
                          log(3) = "Files not old enough"
                          InsertLog(log)
                      End If
                      files = Nothing
                  Else
                      log(1) = data(1)
                      InsertLog(log)
                  End If
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-05
        • 2021-07-02
        • 1970-01-01
        • 2012-06-11
        • 1970-01-01
        • 2016-10-04
        相关资源
        最近更新 更多