这会更短吗?

时间:2019-03-20 13:36:34

标签: vb.net

此代码太长。如何缩短时间?每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

2 个答案:

答案 0 :(得分:0)

由于这些函数的功能几乎相同,因此建议您将所有static函数和参数移至所有参数,并将所有static函数/变量名称移至切换用例块或列表。

例如您可以用一个名为ScanareLinia的函数替换接受扫描行的函数。像这样:ScanareLinia2(lineToScan As Integer)。 根据{{​​1}}变量,可以选择要使用的lineToScan。您可以使用select-case statement

答案 1 :(得分:0)

尝试一下...传递1-6:

Private Sub ScanareLinia(ByVal number As Integer)
    Dim mappedNumber As Integer = If(number Mod 3 = 0, 3, number Mod 3)
    Dim tbString As Control = Me.Controls.Find("TxtStringNum" & mappedNumber, True).FirstOrDefault
    Dim tbResults As Control = Me.Controls.Find("TxtResultStr" & mappedNumber, True).FirstOrDefault
    If Not IsNothing(tbString) AndAlso Not IsNothing(tbResults) Then
        Dim textsrtring As String = tbString.Text
        Dim words As String() = textsrtring.Split(New Char() {" "c}) ' Split string based on space
        For Each word As Integer In words ' Use For Each loop over words
            For i As Integer = 0 To txtIntDraws.Lines.Count - 1
                Select Case number
                    Case 1 To 3
                        If TxtIntDraws.Lines(1).Contains(word) Then
                            If Array.IndexOf(TxtIntDraws.Lines(1).Split(","c), CStr(word)) > -1 Then
                                tbResults.Text = word
                            End If
                        End If

                    Case 4 To 6
                        If Array.IndexOf(TxtIntDraws.Lines(2).Split(","c), CStr(word)) > -1 Then
                            tbResults.Text = word
                        End If

                    Case Else
                        MessageBox.Show("Now what?!")

                End Select
            Next
        Next
        If mappedNumber = 3 Then
            TxtDrawRemize.Text &= TxtResultStr1.Text + " " + TxtResultStr2.Text + " " + TxtResultStr3.Text & Environment.NewLine
        End If
    End If
End Sub
相关问题