我有变量el1,el2,...... el16和c1,c2,.... c16和Counter 想在计数循环中使用
For Counter = 1 to 16
If el1 = "x" Then
el1 = 0
c1 = 0
Else
c1 = 1
End If
Next
If语句适用于已定义的变量。如何将“计数器”变量与循环中的“el”和“c”组合以识别为el1,c1,el2,c2,... el16,c16
Sub findelement
Dim el1, el2, el3, el4, el5, el6, el7, el8, el9, el10, el11, el12, el13, el14, el15, el16, scoreA, scoreB, calc
el1 = EDITFORM.Pages.Item("page4").Controls.Item("element1")
el2 = EDITFORM.Pages.Item("page4").Controls.Item("element2")
el3 = EDITFORM.Pages.Item("page4").Controls.Item("element3")
el4 = EDITFORM.Pages.Item("page4").Controls.Item("element4")
el5 = EDITFORM.Pages.Item("page4").Controls.Item("element5")
el6 = EDITFORM.Pages.Item("page4").Controls.Item("element6")
el7 = EDITFORM.Pages.Item("page4").Controls.Item("element7")
el8 = EDITFORM.Pages.Item("page4").Controls.Item("element8")
el9 = EDITFORM.Pages.Item("page5").Controls.Item("element9")
el10 = EDITFORM.Pages.Item("page5").Controls.Item("element10")
el11 = EDITFORM.Pages.Item("page5").Controls.Item("element11")
el12 = EDITFORM.Pages.Item("page5").Controls.Item("element12")
el13 = EDITFORM.Pages.Item("page5").Controls.Item("element13")
el14 = EDITFORM.Pages.Item("page5").Controls.Item("element14")
el15 = EDITFORM.Pages.Item("page5").Controls.Item("element15")
el16 = EDITFORM.Pages.Item("page5").Controls.Item("element16")
Dim c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13, c14, c15, c16, Counter
For Counter = 1 to 16
If "x" = eval ("el" & Counter) Then
execute "el" & Counter & " = 0"
execute "c" & Counter & " = 0"
Else
execute "c" & Counter & " = 1"
End If
Next
'do math
scoreA = CInt(el1) + CInt(el2) + CInt(el3) + CInt(el4) + CInt(el5) + CInt(el6) + CInt(el7) + CInt(el8) + CInt(e9) + CInt(el10) + CInt(el11) + CInt(el12) + CInt(el13) + CInt(el14) + CInt(el15) + CInt(el16)
scoreB = CInt(c1) + CInt(c2) + CInt(c3) + CInt(c4) + CInt(c5) + CInt(c6) + CInt(c7) + CInt(c8) + CInt(c9) + CInt(c10) + CInt(c11) + CInt(c12) + CInt(c13) + CInt(c14) + CInt(c15) + CInt(c16)
calc = CDbl(scoreA) / CDbl(scoreB)
'vbscript assumes these are strings and appends them, to get the sum like you want cast them to a double (CDbl) or integer (CInt)
'MsgBox (scoreA)
'MsgBox (scoreB)
'MsgBox (calc)
'Return calculated data to form.....
EDITFORM.Pages("Results").Controls("sumEl").Value = (scoreA)
EDITFORM.Pages("Results").Controls("scoreB").Value = (scoreB)
EDITFORM.Pages("Results").Controls("calc").Value = (calc)
End Sub
答案 0 :(得分:1)
您可以使用Execute
,ExecuteGlobal
和Eval
动态创建要执行的表达式的变量名称:
Execute "el" & Counter & " = 42"
答案 1 :(得分:1)
通常,Eval / Execute / ExecuteGlobal是危险的,效率低下的 不必要。有异常(例如库/模块导入,(快速和脏) 基于用户输入的计算),但我怀疑你的(真实)问题不可能 没有这样的伏都教就解决了。
比较
Dim el1, el2, el3
el1 = "a"
el2 = "x"
el3 = "b"
WScript.Echo "el:", el1, el2, el3
Dim c1, c2, c3
Dim i
For i = 1 to 3
If "x" = Eval("el" & i) Then
Execute "el" & i & " = 0"
Execute "c" & i & " = 0"
Else
Execute "c" & i & " = 1"
End If
Next
WScript.Echo "el:", el1, el2, el3
WScript.Echo " c:", c1, c2, c3
output:
el: a x b
el: a 0 b
c: 1 0 1
(从海伦偷来的想法)
使用:
Dim sEL : sEL = "a x b"
Dim oWAN : Set oWAN = WScript.Arguments.Named
If oWAN.Exists("el") Then sEL = oWAN("el")
Dim aEL : aEL = Split(sEL)
WScript.Echo "aEL:", Join(aEL)
ReDim aC(UBound(aEL))
Dim i
For i = 0 to UBound(aEL)
If "x" = aEL(i) Then
aEL(i) = 0
aC(i) = 0
Else
aC(i) = 1
End If
Next
WScript.Echo "aEL:", Join(aEL)
WScript.Echo " aC:", Join(aC)
output (no arg):
aEL: a x b
aEL: a 0 b
aC: 1 0 1
output (arg: /el:"a a a x b x x c x"):
aEL: a a a x b x x c x
aEL: a a a 0 b 0 0 c 0
aC: 1 1 1 0 1 0 0 1 0
我认为,您同意使用集合/数组可以提供更多灵活性 作为评估/执行方法。