mardi 19 mai 2015

Stumped why I'm getting "System.ArgumentOutOfRangeException" error

I've run into a problem with a program for my Visual Basic class that has me stumped. The assignment is a Guessing Game, I'm to create a private sub that generates a random number within the bounds of a user input (ie... 1 - 100). I seem to have this functional with the exception of telling the computer to guess lower. Currently if I press "Higher" the game continues, but if I press "Lower" the error is thrown with "minValue cannot exceed maxValue". If the output in the text box is higher than my lownum variable, why am I getting this error? The bolded code is the code I have written, everything else was provided.

Public Class frmGuess
Dim highNum As Integer
Dim lowNum As Integer
Dim guess As Integer

Private Sub frmGuess_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    MessageBox.Show("Lets Play a Game!")
    'Fixed the grammatical error below - "Think of an whole number" is incorrect.
    MessageBox.Show("Think of a whole number -- DON'T TELL ME!")
    MessageBox.Show("Now give me a range you want me to guess in... like 1 to 100.")
    lowNum = InputBox("Whats the starting value?", "Lowest Value")
    highNum = InputBox("Whats the ending value?", "Highest Value")
    lblTitle.Text = "I am guessing a number between " & lowNum & " and " & highNum & "."
    newGuess()
End Sub

**Sub newGuess()
    '
    '   ITS GONE! OH NO!
    '
    'New Guess Sub - Calculates a response based on the input from user and recalculates responses to guess the number.
    'Setting New Variables
    Dim random As New Random

    'Generating Response
    txtGuess.Clear()
    txtGuess.Text = random.Next(lowNum, highNum + 1)

End Sub**

Private Sub btnLower_Click(sender As Object, e As EventArgs) Handles btnLower.Click
    highNum = guess - 1
    newGuess()
End Sub

Private Sub btnHigher_Click(sender As Object, e As EventArgs) Handles btnHigher.Click
    lowNum = guess + 1
    newGuess()
End Sub

Private Sub btnCorrect_Click(sender As Object, e As EventArgs) Handles btnCorrect.Click
    MessageBox.Show("I win. I told you I was awesome.")
    Close()
End Sub

Private Sub txtGuess_TextChanged(sender As Object, e As EventArgs) Handles txtGuess.TextChanged

End Sub

End Class




Aucun commentaire:

Enregistrer un commentaire