#vb.net
#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 amp;= Environment.NewLine amp; 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 amp;= Environment.NewLine amp; 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 amp;= Environment.NewLine amp; 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:
Обратите внимание на то, что меняется в каждой версии:
Dim secondBoxList = TxtNumberListScan.Lines(1).Split(" ")
Dim secondBoxList = TxtNumberListScan.Lines(2).Split(" ")
Dim secondBoxList = TxtNumberListScan.Lines(3).Split(" ")
ListBoxRemize1
ListBoxRemize2
ListBoxRemize3
Итак, вы можете использовать массив ссылок на ListBoxRemize
s и цикл 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 amp;= Environment.NewLine amp; 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)
.