在VB.Net函数中编码较短的交集

时间:2019-04-09 08:46:57

标签: vb.net

如何缩短此时间?我想在每个TxtNumberListScan.Lines (i) .Split ("")之后计算我的值,而不是全部。这就是为什么我没有设法缩短代码,可以缩短代码吗?

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Try
        Dim intersectionList As New List(Of String)()
        For i As Integer = 1 To TxtCheckDraws.Text
            Dim firstBoxList = TxtBoxIntDraws.Lines(i).Split(",")
            Dim secondBoxList = TxtNumberListScan.Lines(1).Split(" ")
            intersectionList.Add(String.Join(",", secondBoxList.Intersect(firstBoxList)))
        Next
        TxtMReadOnly.Text = String.Join(Environment.NewLine, intersectionList)
        TxtNumberListCount.Text &= Environment.NewLine & TxtMReadOnly.Text.Where(Function(x) Not Char.IsWhiteSpace(x)).Count()
        While ListBoxRemize1.Items.Count > 0
            ListBoxRemize1.Items.RemoveAt(ListBoxRemize1.Items.Count - 1)
        End While
        For i As Integer = 0 To TxtMReadOnly.Lines.Count - 1
            ListBoxRemize1.Items.Add(TxtMReadOnly.Lines(i))
        Next
        TxtMReadOnly.Clear()
    Catch ex As Exception
    End Try
    Try
        Dim intersectionList As New List(Of String)()
        For i As Integer = 1 To TxtCheckDraws.Text
            Dim firstBoxList = TxtBoxIntDraws.Lines(i).Split(",")
            Dim secondBoxList = TxtNumberListScan.Lines(2).Split(" ")
            intersectionList.Add(String.Join(",", secondBoxList.Intersect(firstBoxList)))
        Next
        TxtMReadOnly.Text = String.Join(Environment.NewLine, intersectionList)
        TxtNumberListCount.Text &= Environment.NewLine & TxtMReadOnly.Text.Where(Function(x) Not Char.IsWhiteSpace(x)).Count()
        While ListBoxRemize2.Items.Count > 0
            ListBoxRemize2.Items.RemoveAt(ListBoxRemize2.Items.Count - 1)
        End While
        For i As Integer = 0 To TxtMReadOnly.Lines.Count - 1
            ListBoxRemize2.Items.Add(TxtMReadOnly.Lines(i))
        Next
        TxtMReadOnly.Clear()
    Catch ex As Exception
    End Try
    Try
        Dim intersectionList As New List(Of String)()
        For i As Integer = 1 To TxtCheckDraws.Text
            Dim firstBoxList = TxtBoxIntDraws.Lines(i).Split(",")
            Dim secondBoxList = TxtNumberListScan.Lines(3).Split(" ")
            intersectionList.Add(String.Join(",", secondBoxList.Intersect(firstBoxList)))
        Next
        TxtMReadOnly.Text = String.Join(Environment.NewLine, intersectionList)
        TxtNumberListCount.Text &= Environment.NewLine & TxtMReadOnly.Text.Where(Function(x) Not Char.IsWhiteSpace(x)).Count()
        While ListBoxRemize3.Items.Count > 0
            ListBoxRemize3.Items.RemoveAt(ListBoxRemize3.Items.Count - 1)
        End While
        For i As Integer = 0 To TxtMReadOnly.Lines.Count - 1
            ListBoxRemize3.Items.Add(TxtMReadOnly.Lines(i))
        Next
        TxtMReadOnly.Clear()
    Catch ex As Exception
    End Try

1 个答案:

答案 0 :(得分:2)

寻找每个版本中发生变化的内容:

Dim secondBoxList = TxtNumberListScan.Lines(1).Split(" ")
Dim secondBoxList = TxtNumberListScan.Lines(2).Split(" ")
Dim secondBoxList = TxtNumberListScan.Lines(3).Split(" ")
ListBoxRemize1
ListBoxRemize2
ListBoxRemize3

因此,您可以使用对ListBoxRemize的引用数组和一个For循环来生成索引:

Option Infer On
'..............

Dim ListBoxRemizes = {ListBoxRemize1, ListBoxRemize2, ListBoxRemize3}

For j = 0 To ListBoxRemizes.Length - 1
    Dim intersectionList As New List(Of String)()

    For i As Integer = 1 To TxtCheckDraws.Text
        Dim firstBoxList = TxtBoxIntDraws.Lines(i).Split(",")
        Dim secondBoxList = TxtNumberListScan.Lines(j + 1).Split(" ")
        intersectionList.Add(String.Join(",", secondBoxList.Intersect(firstBoxList)))
    Next

    TxtMReadOnly.Text = String.Join(Environment.NewLine, intersectionList)
    TxtNumberListCount.Text &= Environment.NewLine & TxtMReadOnly.Text.Where(Function(x) Not Char.IsWhiteSpace(x)).Count()

    ListBoxRemizes(j).Items.Clear()

    For i As Integer = 0 To TxtMReadOnly.Lines.Count - 1
        ListBoxRemizes(j).Items.Add(TxtMReadOnly.Lines(i))
    Next

    TxtMReadOnly.Clear()

Next

我用Clear()代替了看起来很长的代码。另外,看起来您可以使用AddRange而不是循环来添加这些行,有点像:ListBoxRemizes(j).Items.AddRange(TxtMReadOnly.Lines)