1

I'm trying to create a little powershell program which asks you a random word and you have to translate it. I'm using the index of both arrays to determine if the answer is correct or not.

Everytime the user enters a correct or wrong answer a textbox should increase it's value to show how many the user got right and wrong so far. The program works as it should so far but the counter isn't. At this moment the counter increments only to 1 and doesn't go above 1. Am i missing something?

I think the problem could be that PowerShell always calls the value from the external countercorrect and counterwrong which is 0 but how could i solve it that it only calls the incremented value?

$QuestionArray = New-Object System.Collections.ArrayList
$AnswerArray = New-Object System.Collections.ArrayList
$countercorrect = 0
$counterwrong = 0

#word array
$QuestionArray.Add("word1")
$QuestionArray.Add("word2")
$QuestionArray.Add("word3")

#solution array
$AnswerArray.Add("answer1")
$AnswerArray.Add("answer2")
$AnswerArray.Add("answer3")

#Function to display a new word
function Question {
    $global:RandomQuestion = $QuestionArray | Get-Random
    $SearchedTextbox.Text = $global:RandomQuestion
}

$InputTextbox.Add_KeyDown({
    if ($_.KeyCode -eq "Enter") {
        #Get User Guess
        $Answer = $InputTextbox.Text
        #Get Solution array Index
        $IndexPositionQuestion = [array]::indexof($QuestionArray, $global:RandomQuestion)
        #Get User answer array Index
        $IndexPositionAnswer = [array]::indexof($AnswerArray, $Answer)
        #Check if both indexes match
        If($IndexPositionAnswer -eq $IndexPositionQuestion){

            #this fails / doesn't go above 1
            $RightTextBox.Text = countercorrect++
            Question
        }else{

            #this fails / doesn't go above 1
            $WrongtTextBox.Text = counterwrong++ 
            Question
        }
    }
})

I tried using a seperate function to increment its value but even that only increased to 1.

3
  • 1
    The code is missing some variable identifiers : $RightTextBox.Text = countercorrect++ . Are these just a copy-paste-edit error? Commented Jan 3, 2017 at 14:17
  • 1
    $countercorrect++ -> $script:countercorrect++, $counterwrong++ -> $script:counterwrong++. You are in a function's scope when increasing/decreasing the counter (untested but I'm pretty confident). And yeah as @vonPryz mentionned, you miss a couple $ signs. Commented Jan 3, 2017 at 14:17
  • Thanks! yeah my fault they were indeed copy paste errors. @sodawillow thank you very much for the solution i tried using $script:countercorrect++ before but only inside another function and it just deleted the entire value for some reason. But using it outside a function works like a charm! Commented Jan 3, 2017 at 14:23

1 Answer 1

2

Simplified example:

$form = New-Object System.Windows.Forms.Form

$counter = 0

$button = New-Object System.Windows.Forms.Button
$button.Text = "button"
$button.Add_Click({

    $script:counter++

})

$form.Controls.Add($button)

$form.ShowDialog()

$counter

If you replace $script:counter++ with $counter++, the value is not incremented properly.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.