这是来自Visual Basic程序的代码,我想知道是否有办法简化它?
它需要字符串的数量并表示,如果你等于这个数字,那就显示这封信和这个封面。
有没有更简单的方法来执行此操作,因此我不必一遍又一遍地重复代码?
If UBound(Variables.Words) = "0" Then
Round1.Letter6.Show()
Round1.Cover6.Show()
ElseIf UBound(Variables.Words) = "1" Then
Round1.Letter6.Show()
Round1.Cover6.Show()
Round1.Letter7.Show()
Round1.Cover7.Show()
ElseIf UBound(Variables.Words) = "2" Then
Round1.Letter5.Show()
Round1.Cover5.Show()
Round1.Letter6.Show()
Round1.Cover6.Show()
Round1.Letter7.Show()
Round1.Cover7.Show()
ElseIf UBound(Variables.Words) = "3" Then
Round1.Letter5.Show()
Round1.Cover5.Show()
Round1.Letter6.Show()
Round1.Cover6.Show()
Round1.Letter7.Show()
Round1.Cover7.Show()
Round1.Letter8.Show()
Round1.Cover8.Show()
ElseIf UBound(Variables.Words) = "4" Then
Round1.Letter4.Show()
Round1.Cover4.Show()
Round1.Letter5.Show()
Round1.Cover5.Show()
Round1.Letter6.Show()
Round1.Cover6.Show()
Round1.Letter7.Show()
Round1.Cover7.Show()
Round1.Letter8.Show()
Round1.Cover8.Show()
ElseIf UBound(Variables.Words) = "5" Then
Round1.Letter4.Show()
Round1.Cover4.Show()
Round1.Letter5.Show()
Round1.Cover5.Show()
Round1.Letter6.Show()
Round1.Cover6.Show()
Round1.Letter7.Show()
Round1.Cover7.Show()
Round1.Letter8.Show()
Round1.Cover8.Show()
Round1.Letter9.Show()
Round1.Cover9.Show()
ElseIf UBound(Variables.Words) = "6" Then
Round1.Letter3.Show()
Round1.Cover3.Show()
Round1.Letter4.Show()
Round1.Cover4.Show()
Round1.Letter5.Show()
Round1.Cover5.Show()
Round1.Letter6.Show()
Round1.Cover6.Show()
Round1.Letter7.Show()
Round1.Cover7.Show()
Round1.Letter8.Show()
Round1.Cover8.Show()
Round1.Letter9.Show()
Round1.Cover9.Show()
ElseIf UBound(Variables.Words) = "7" Then
Round1.Letter3.Show()
Round1.Cover3.Show()
Round1.Letter4.Show()
Round1.Cover4.Show()
Round1.Letter5.Show()
Round1.Cover5.Show()
Round1.Letter6.Show()
Round1.Cover6.Show()
Round1.Letter7.Show()
Round1.Cover7.Show()
Round1.Letter8.Show()
Round1.Cover8.Show()
Round1.Letter9.Show()
Round1.Cover9.Show()
Round1.Letter10.Show()
Round1.Cover10.Show()
ElseIf UBound(Variables.Words) = "8" Then
Round1.Letter2.Show()
Round1.Cover2.Show()
Round1.Letter3.Show()
Round1.Cover3.Show()
Round1.Letter4.Show()
Round1.Cover4.Show()
Round1.Letter5.Show()
Round1.Cover5.Show()
Round1.Letter6.Show()
Round1.Cover6.Show()
Round1.Letter7.Show()
Round1.Cover7.Show()
Round1.Letter8.Show()
Round1.Cover8.Show()
Round1.Letter9.Show()
Round1.Cover9.Show()
Round1.Letter10.Show()
Round1.Cover10.Show()
ElseIf UBound(Variables.Words) = "9" Then
Round1.Letter2.Show()
Round1.Cover2.Show()
Round1.Letter3.Show()
Round1.Cover3.Show()
Round1.Letter4.Show()
Round1.Cover4.Show()
Round1.Letter5.Show()
Round1.Cover5.Show()
Round1.Letter6.Show()
Round1.Cover6.Show()
Round1.Letter7.Show()
Round1.Cover7.Show()
Round1.Letter8.Show()
Round1.Cover8.Show()
Round1.Letter9.Show()
Round1.Cover9.Show()
Round1.Letter10.Show()
Round1.Cover10.Show()
Round1.Letter11.Show()
Round1.Cover11.Show()
ElseIf UBound(Variables.Words) = "10" Then
Round1.Letter1.Show()
Round1.Cover1.Show()
Round1.Letter2.Show()
Round1.Cover2.Show()
Round1.Letter3.Show()
Round1.Cover3.Show()
Round1.Letter4.Show()
Round1.Cover4.Show()
Round1.Letter5.Show()
Round1.Cover5.Show()
Round1.Letter6.Show()
Round1.Cover6.Show()
Round1.Letter7.Show()
Round1.Cover7.Show()
Round1.Letter8.Show()
Round1.Cover8.Show()
Round1.Letter9.Show()
Round1.Cover9.Show()
Round1.Letter10.Show()
Round1.Cover10.Show()
Round1.Letter11.Show()
Round1.Cover11.Show()
ElseIf UBound(Variables.Words) = "11" Then
Round1.Letter1.Show()
Round1.Cover1.Show()
Round1.Letter2.Show()
Round1.Cover2.Show()
Round1.Letter3.Show()
Round1.Cover3.Show()
Round1.Letter4.Show()
Round1.Cover4.Show()
Round1.Letter5.Show()
Round1.Cover5.Show()
Round1.Letter6.Show()
Round1.Cover6.Show()
Round1.Letter7.Show()
Round1.Cover7.Show()
Round1.Letter8.Show()
Round1.Cover8.Show()
Round1.Letter9.Show()
Round1.Cover9.Show()
Round1.Letter10.Show()
Round1.Cover10.Show()
Round1.Letter11.Show()
Round1.Cover11.Show()
Round1.Letter12.Show()
Round1.Cover12.Show()
End If
非常感谢任何帮助!
谢谢,Dan。
编辑** 我不是新手,但我只懂基本功能。抱歉很难,但是你能告诉我我需要做的全部例子。
再次感谢。
答案 0 :(得分:1)
你能重构这些字母和封面,以便它们在一个数组或集合中,然后假设它们总是连续的,写一个程序,沿着这些行传递开始和结束参数:
dim i as integer = Ubound(words)
select case i
case 0
showlettersandcovers(6,6)
case 1
showlettersandcovers(6,7)
. . .
case 3
showlettersandcovers(5,8)
. . .
(在回复评论时添加)
sub ShowLettersAndcovers(startnum as integer, endnum as integer)
for i as integer = startnum to endnum
Round1.Letter(i).show
Round1.Cover(i).show
next
假设您可以将您的信件和封面分成数组或类似内容 - 在不知道申请细节的情况下,我不知道这是否是一个合理的假设。例如,您可以将这些字母设置为:
Dim letterarray as letter() with {new letter(paramsforletter0), new letter(paramsforletter1 . . . }
或者您可能正在从数据库加载它们,因此它们可能已经存在于您可以如上所述迭代的数据集中。 。
答案 1 :(得分:0)
稍微减少重复的另一个选择是将调用分组到函数中,因为看起来字母和封面总是一起显示。
Private Function ShowSection6()
Round1.Letter6.Show()
Round1.Cover6.Show()
End Sub
Private Function ShowSection7()
Round1.Letter7.Show()
Round1.Cover7.Show()
End Sub
'.... etc ...
然后在@Westie提到的Select语句中对事物进行分组,因为每次只有一个结果
Select Case UBound(Variables.Words)
Case 0
ShowSection6()
Case 1
ShowSection6()
ShowSection7()
Case 2
...
Case Else
...
End Select
答案 2 :(得分:0)
以Peter G的想法为基础:
你可以避免你必须命名这么多变量的情况,以便能够通过在下面显示的LoadItemLookup方法中动态创建它们来控制它们,但是按照你的方式执行它你可以使用查找像这样:
' Lookup of Cover/Letter objects
Private _ItemLookup as Dictionary(of Integer, YourObjectType)
Private ReadOnly Property ItemLookup As Dictionary(of Integer, YourObjectType)
Get
If IsNothing(_itemLookup) Then
_ItemLookup = LoadItemLookup
End If
Return _ItemLookup
End Get
End Property
' Loads the lookup
Private function LoadItemLookup as Dictionary(of Integer, YourObjectType)
dim newLookup As New Dictionary(of Integer, YourObjectType)
newLookup.Add(1, Round1.Letter1)
newLookup.Add(2, Round1.Letter2)
newLookup.Add(3, Round1.Letter3)
'etc
Return newLookup
End Sub
Private Sub YourExecutingSub
Select Case UBound(Variables.Words)
Case "0"
ShowItems(6, 6)
Case "1"
ShowItems(6, 7)
Case "2"
ShowItems(5, 7)
Case "3"
ShowItems(5, 8)
Case "4"
ShowItems(4, 8)
End Select
End Sub
' taken from peter g's answer
Private Sub ShowItems(ByVal startNumber as Integer, byval EndNumber as Integer)
For i As Integer = StartNumber to EndNUmber
If ItemLookup.ContainsKey(i) Then
ItemLookup(i).Letter.Show
ItemLookup(i).Cover.Show
End If
Next
End Sub