2013-06-22 43 views
1

我确定有一种我不知道如何帮助的巧妙技巧,但我正在努力寻找任何接近的例子,所以我希望有人能帮助指点我正确的方向。在VBScript中编辑全局变量

我有一些全局变量,我想从一个子程序内编辑它们,具体取决于传入的变量。

基本上,这里的想法(在更大规模的做法虽然):

<Script Language="VBScript"> 
    game1won=0 
    game1full=0 
    game2won=0 
    game2full=0 

    Sub Game11 
     playerMove 1,1 
    End Sub 

    Sub Game12 
     playerMove 1,2 
    End Sub 

    Sub Game21 
     playerMove 2,1 
    End Sub 

    Sub Game22 
     playerMove 2,2 
    End Sub 

    Sub playerMove(firstNumber, secondNumber) 
     If [code to check if game is won] Then 
      game[firstNumber]won=1 
     End If 
    End Sub 
</Script> 
<Body> 
    <input id=runButton type="button" value="1.1" onClick="Game11><br> 
    <input id=runButton type="button" value="1.2" onClick="Game12><br> 
    <input id=runButton type="button" value="2.1" onClick="Game21><br> 
    <input id=runButton type="button" value="2.2" onClick="Game22><br> 

</Body> 

正如你所看到的,我想编辑包含传递到子的PlayerMove第一个数字变量,但不管我在尝试什么,我都不断创建新的变量,而不是编辑现有的全局变量。

有没有一种聪明的方式来编辑这个没有负载的IF/CASE语句,可以在这里帮助?

谢谢你们!

回答

2

我不同意“你不能用vbscript做这个”声明。看看ExecuteGlobal

game1won = 0 
playerMove 1 
MsgBox game1won 
Sub playerMove(firstNumber) 
    ExecuteGlobal "game" & firstNumber & "won=1" 
End Sub 
0

不,你不能用vbscript做到这一点。最好的选择是使用阵列:

Dim gameWon(2) 
Dim gameFull(2) 

gameWon(0) = 0 
gameWon(1) = 0 
gameFull(0) = 0 
gameFull(1) = 0 


Sub playerMove(firstNumber, secondNumber) 
    If [code to check if game is won] Then 
     gameWon(firstNumber-1)=1 
    End If 
End Sub 
+0

完美的,正是我寻找的答案。我从来没有想过要使用数组,谢谢! – Ekins86