michaelekins
michaelekins

Reputation: 579

Editing global variables in VBScript

I'm sure there is a clever technique I don't know about to help with this, but I'm struggling to find any close examples so I'm hoping someone can help point me in the correct direction.

I have some global variables and I want to edit them from within a Subroutine depending on what variables are passed into it.

Basically, here is the idea (although in practice on a much larger scale):

<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>

As you can see, I want to edit the variable containing the first number passed into the sub playerMove, but no matter what I'm trying I keep creating new variables rather than editing the existing global one.

Is there a clever way to edit this without loads of IF/CASE statements that can help here?

Thanks guys!

Upvotes: 1

Views: 1455

Answers (2)

Panayot Karabakalov
Panayot Karabakalov

Reputation: 3179

I can't agree with "you can't do that with vbscript" statement. Take a look at ExecuteGlobal.

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

Upvotes: 2

MaxiWheat
MaxiWheat

Reputation: 6251

No, you can't do that with vbscript. The best alternative would be to use arrays :

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

Upvotes: 0

Related Questions