优化For循环和Do to循环的最佳方法

时间:2018-02-23 10:54:04

标签: vb.net loops for-loop do-loops

我有以下代码搜索DataGridView表中的文件夹目录,并将所需格式的所有文件放入列表中,它还会收集上次修改日期的列表,以便以后在应用程序中使用。

代码有效,但眼睛疼痛。我想整理以下循环以提高效率 - 我的意思是我在For循环中有一个For循环,它创建了文件名列表,然后我有两个单独的Do Until循环,从头到尾搜索列表挑出需要调整的文件名。

我非常有兴趣学习一种更好的方法来实现相同的结果,因为我对编码效率的了解非常重要。基本上,这可以在一个或两个循环中完成,因为循环遍历列表两次的想法似乎效率低下了吗?

Public Class
    Private Sub btnDirectory_Click(sender As Object, e As EventArgs) Handles btnDirectory.Click

    Dim FileNames As New List(Of String)
    Dim FileDates As New List(Of Date)

    Dim DocNo As String
    Dim rowCheck As String
    Dim ProjectNo As String = "1111"
    Dim FileNameCheck As String
    Dim str As String

    Dim k As Integer = 0 
    Dim i As Integer
    Dim j As Integer

    Dim CorrectType As Boolean = False

    'The first loop grabs all files of the wanted format from a datagridview table containing all directories to be checked

    For Each rw In Background.Table1.Rows

        rowCheck = Background.Table1(0, k).Value
        If Not String.IsNullOrEmpty(rowCheck) Then

        For Each file As String In My.Computer.FileSystem.GetFiles(Background.Table1(0, k).Value)
            CorrectType = False
            FileNameCheck = IO.Path.GetFileNameWithoutExtension(file)
            If FileNameCheck.Contains(ProjectNo) AndAlso FileNameCheck.Contains("-") AndAlso Not String.IsNullOrEmpty(FileNameCheck) AndAlso FileNameCheck.Contains(" ") Then
                DocNo = FileNameCheck.Substring(0, FileNameCheck.IndexOf(" "))
                If FileNameCheck.Substring(0, FileNameCheck.IndexOf("-")) = ProjectNo AndAlso CountLetters(DocNo) = 3 Then
                    CorrectType = True                                   
                End If
            End If
            If CorrectType = True Then
                FileNames.Add(FileNameCheck)
                FileDates.Add(IO.File.GetLastWriteTime(file))
            End If

        Next
        End If

        k += 1

    Next

    'The next loop tidies up the file formats that contain a "-00-" in their names

     j = FileNames.Count
     i = 0
     Do
         str = FileNames(i)
         If str.Contains("-00-") Then                           
             FileNames(i) = RemoveChar(str, "-00-") ' RemoveChar is a function that replaces "-00-" with a "-"
         End If
         i += 1
     Loop Until i = j

     i = 0
     j = FileNames.Count


     'Finally, this loop checks that no two files have the exact same name, and gets rid of one of them if that is the case

     Do

         Dim st1 As String = FileNames(j - 1)
         Dim st2 As String = FileNames(j - 2)

         If st1 = st2 Then          
             FileNames.RemoveAt(j - 1)
             FileDates.RemoveAt(j - 1)
         End If
         j -=  1
     Loop Until j = 1


     End Sub


End Class

1 个答案:

答案 0 :(得分:1)

代码肯定很难看。

For Each rw循环不使用rw。您可以使用以下循环替换它:

For k = 1 to Background.Table1.Rows.Count
    ' Do things here
Next k

您指定了rowCheck并使用了一次,但您错过了在For Each file行重复使用它的机会。

如果您拥有CorrectType = True,则可以轻松放置相应的代码。

        If FileNameCheck.Substring(0, FileNameCheck.IndexOf("-")) = ProjectNo AndAlso CountLetters(DocNo) = 3 Then
            CorrectType = True                                   
        End If
    End If
    If CorrectType = True Then
        FileNames.Add(FileNameCheck)
        FileDates.Add(IO.File.GetLastWriteTime(file))
    End If

变为:

        If FileNameCheck.Substring(0, FileNameCheck.IndexOf("-")) = ProjectNo AndAlso CountLetters(DocNo) = 3 Then
            FileNames.Add(FileNameCheck)
            FileDates.Add(IO.File.GetLastWriteTime(file))
        End If

我必须承认,接下来的两个循环使我的眼睛流血(比喻,而不是字面意思)。

 j = FileNames.Count
 i = 0
 Do
     str = FileNames(i)
     If str.Contains("-00-") Then                           
         FileNames(i) = RemoveChar(str, "-00-") ' RemoveChar is a function that replaces "-00-" with a "-"
     End If
     i += 1
 Loop Until i = j

变为

 for i = 1 to FileNames.Count
     str = FileNames(i)
     If str.Contains("-00-") Then                           
         FileNames(i) = RemoveChar(str, "-00-") ' RemoveChar is a function that replaces "-00-" with a "-"
     End If
 Next I

    i = 0
    j = FileNames.Count
 'Finally, this loop checks that no two files have the exact same name, and gets rid of one of them if that is the case

 Do

     Dim st1 As String = FileNames(j - 1)
     Dim st2 As String = FileNames(j - 2)

     If st1 = st2 Then          
         FileNames.RemoveAt(j - 1)
         FileDates.RemoveAt(j - 1)
     End If
     j -=  1
 Loop Until j = 1

变为

 'Finally, this loop checks that no two files have the exact same name, and gets rid of one of them if that is the case

 For j = FileNames.Count - 1 to 1 Step -1 ' Check my counting here - stop at 1, 2 or 0?

     Dim st1 As String = FileNames(j)
     Dim st2 As String = FileNames(j - 1)

     If st1 = st2 Then          
         FileNames.RemoveAt(j)
         FileDates.RemoveAt(j)
     End If
 Next j