首页> 疑难解答

这会更短吗?

withpy 2021-06-19

简介这段代码太长了。如何缩短它?非常3中3,这将出现在最后。 TxtDrawRemize.Text&= TxtResultStr1.Text +“”+ TxtResultStr2.Text +“”+ TxtResultStr3.Text&...

这段代码太长了。如何缩短它?非常3中3,这将出现在最后。 TxtDrawRemize.Text & = TxtResultStr1.Text + "" + TxtResultStr2.Text + "" + TxtResultStr3.Text & Environment.NewLine

我想这么做,我陷入了代码中,因为它太长了,有时候我会误解变量。如果你能帮助我,我将不胜感激。

 Private Sub ScanareLinia1()
        On Error Resume Next
        Dim textsrtring As String = TxtStringNum1.Text
        Dim words As String() = textsrtring.Split(" "c)
        ' Split string based on space
        Dim found As Boolean = False
        ' Use For Each loop over words
        Dim word As Integer
        For Each word In words
            For i As Integer = 0 To TxtIntDraws.Lines.Count - 1
                If TxtIntDraws.Lines(1).Contains(word) Then
                    If Array.IndexOf(TxtIntDraws.Lines(1).Split(","c), CStr(word)) > -1 Then
                        TxtResultStr1.Text = word
                    End If
                End If
            Next
        Next
    End Sub
    Private Sub ScanareLinia2()
        On Error Resume Next
        Dim textsrtring As String = TxtStringNum2.Text
        Dim words As String() = textsrtring.Split(New Char() {" "c})
        ' Split string based on space
        Dim found As Boolean = False
        ' Use For Each loop over words
        Dim word As Integer
        For Each word In words
            For i As Integer = 0 To TxtIntDraws.Lines.Count - 1
                If TxtIntDraws.Lines(1).Contains(word) Then
                    If Array.IndexOf(TxtIntDraws.Lines(1).Split(","c), CStr(word)) > -1 Then
                        TxtResultStr2.Text = word
                    End If
                End If
            Next
        Next
    End Sub
    Private Sub ScanareLinia3()
        On Error Resume Next
        Dim textsrtring As String = TxtStringNum3.Text
        Dim words As String() = textsrtring.Split(New Char() {" "c})
        ' Split string based on space
        Dim found As Boolean = False
        ' Use For Each loop over words
        Dim word As Integer
        For Each word In words
            For i As Integer = 0 To TxtIntDraws.Lines.Count - 1
                If TxtIntDraws.Lines(1).Contains(word) Then
                    If Array.IndexOf(TxtIntDraws.Lines(1).Split(","c), CStr(word)) > -1 Then
                        TxtResultStr3.Text = word
                    End If
                End If
            Next
        Next
        TxtDrawRemize.Text &= TxtResultStr1.Text + " " + TxtResultStr2.Text + " " + TxtResultStr3.Text & Environment.NewLine
    End Sub
    Private Sub ScanareLinia4()
        On Error Resume Next
        Dim textsrtring As String = TxtStringNum1.Text
        Dim words As String() = textsrtring.Split(New Char() {" "c})
        ' Split string based on space
        Dim found As Boolean = False
        ' Use For Each loop over words
        Dim word As Integer
        For Each word In words
            For i As Integer = 0 To TxtIntDraws.Lines.Count - 1
                If Array.IndexOf(TxtIntDraws.Lines(2).Split(","c), CStr(word)) > -1 Then
                    TxtResultStr1.Text = word
                End If
            Next
        Next
    End Sub
    Private Sub ScanareLinia5()
        On Error Resume Next
        Dim textsrtring As String = TxtStringNum2.Text
        Dim words As String() = textsrtring.Split(New Char() {" "c})
        ' Split string based on space
        Dim found As Boolean = False
        ' Use For Each loop over words
        Dim word As Integer
        For Each word In words
            For i As Integer = 0 To TxtIntDraws.Lines.Count - 1
                If Array.IndexOf(TxtIntDraws.Lines(2).Split(","c), CStr(word)) > -1 Then
                    TxtResultStr2.Text = word
                End If
            Next
        Next
    End Sub
    Private Sub ScanareLinia6()
        On Error Resume Next
        Dim textsrtring As String = TxtStringNum3.Text
        Dim words As String() = textsrtring.Split(New Char() {" "c})
        ' Split string based on space
        Dim found As Boolean = False
        ' Use For Each loop over words
        Dim word As Integer
        For Each word In words
            For i As Integer = 0 To TxtIntDraws.Lines.Count - 1
                If Array.IndexOf(TxtIntDraws.Lines(2).Split(","c), CStr(word)) > -1 Then
                    TxtResultStr3.Text = word
                End If
            Next
        Next
        TxtDrawRemize.Text &= TxtResultStr1.Text + " " + TxtResultStr2.Text + " " + TxtResultStr3.Text & Environment.NewLine
    End Sub

相关文章