1

我有变量 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 语句适用于已定义的变量。如何将“Counter”变量与循环中的“el”和“c”结合起来识别为 el1、c1、el2、c2、...el16、c16

这是脚本的最终版本。它在 Windows 7 版本的 ArcPad 中运行良好,但 If 语句在使用移动 vbscript 运行时库的移动设备上爆炸了。可变数组方法可能会更好,但我不太确定如何修补它。任何帮助将不胜感激。

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
4

2 回答 2

1

您可以使用Execute,ExecuteGlobalEval动态形成要执行的表达式的变量名:

Execute "el" & Counter & " = 42"
于 2012-04-27T16:17:34.980 回答
1

一般来说,Eval/Execute/ExecuteGlobal 是危险的、低效的和不必要的。有例外(例如库/模块导入,基于用户输入的(快速和肮脏的)计算),但我怀疑如果没有这样的 Voodoo,你的(真正的)问题就无法解决。

相比:

  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

我认为,您会同意使用集合/数组为您提供与 eval/execute 方法一样多的灵活性。

于 2012-04-27T17:34:00.960 回答