Archived

This topic is now archived and is closed to further replies.

Random in VB.net... errors

This topic is 5129 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Designing a quiz game for a semester project, and near finished. I have the quiz questions, answer options, correct answer and a check to see if it has been played in a 2d array. I use the rnd function to get a random question and check, but sometimes after play I am getting a bad baaad error, and I have no clue why I am getting it. Here is the code in the class that I am running, at least the error that it is showing to go to. The error it gives is "An unhandled exception of type ''System.StackOverflowException'' occurred in microsoft.visualbasic.dll" and highlights the line intRandomQuestion = CInt(Int(6 * Rnd()))
Public Class QuizPlayer

#Region "Declarations and Constructor"
    ''Structure for name and score of each player
    Private Structure PlayerData
        Dim strPlayerName As String     ''Player''s Name Storage
        Dim intPlayerScore As Integer   ''Player''s Score
        Dim chrPlayerAnswer As Char
    End Structure

    ''Structure for output of question to form
    Private Structure CurrentQuestion
        Dim strQuestion As String
        Dim strAnswer1 As String
        Dim strAnswer2 As String
        Dim strAnswer3 As String
        Dim strAnswer4 As String
        Dim strCorrectAnswer As String
        Dim strQuestionUsed As String
    End Structure

    ''All three players data structures defined
    Dim plyPlayer1 As PlayerData
    Dim plyPlayer2 As PlayerData
    Dim plyPlayer3 As PlayerData

    ''One structure of CurrentQuestion
    Dim crqActiveQuestion As CurrentQuestion
    ''Integer for question selection
    Dim intRandomQuestion As Integer

#Region "Question and Answer Array"
    ''Array for Quiz Questions and Answers, uses 2d set up table.
    Dim strQandA(,) As String = { _
    {"Test Question! What is 2 + 2?", "3?", "2?", "4?", "8?", "C", "0"}, _
    {"Test Question! What is 2 + 2?", "3?", "2?", "4?", "8?", "C", "0"}, _
    {"Test Question! What is 2 + 2?", "3?", "2?", "4?", "8?", "C", "0"}, _
    {"Test Question! What is 2 + 2?", "3?", "2?", "4?", "8?", "C", "0"}, _
    {"Test Question! What is 2 + 2?", "3?", "2?", "4?", "8?", "C", "0"}, _
    {"Test Question! What is 2 + 2?", "3?", "2?", "4?", "8?", "C", "0"}, _
    {"Test Question! What is 2 + 2?", "3?", "2?", "4?", "8?", "C", "0"}, _
    {"Test Question! What is 2 + 2?", "3?", "2?", "4?", "8?", "C", "0"} _
    }
#End Region


    ''Constructor

    Sub New()

    End Sub

    Sub New(ByVal newPlayer1Name As String, ByVal newPlayer2Name As String, ByVal newPlayer3Name As String)

        Me.Player1Name = newPlayer1Name
        Me.Player2Name = newPlayer2Name
        Me.Player3Name = newPlayer3Name

    End Sub

#End Region

#Region "Properties"

    Property Player1Name() As String
        Get
            Player1Name = plyPlayer1.strPlayerName
        End Get
        Set(ByVal Value As String)
            plyPlayer1.strPlayerName = Value
        End Set
    End Property

    Property Player2Name() As String
        Get
            Player2Name = plyPlayer2.strPlayerName
        End Get
        Set(ByVal Value As String)
            plyPlayer2.strPlayerName = Value
        End Set
    End Property

    Property Player3Name() As String
        Get
            Player3Name = plyPlayer3.strPlayerName
        End Get
        Set(ByVal Value As String)
            plyPlayer3.strPlayerName = Value
        End Set
    End Property

    ReadOnly Property Player1Score() As Integer
        Get
            Player1Score = plyPlayer1.intPlayerScore
        End Get
    End Property

    ReadOnly Property Player2Score() As Integer
        Get
            Player2Score = plyPlayer2.intPlayerScore
        End Get
    End Property

    ReadOnly Property Player3Score() As Integer
        Get
            Player3Score = plyPlayer3.intPlayerScore
        End Get
    End Property

    Property Player1Answer() As Char
        Get
            Player1Answer = plyPlayer1.chrPlayerAnswer
        End Get
        Set(ByVal Value As Char)
            plyPlayer1.chrPlayerAnswer = Value
        End Set
    End Property

    Property Player2Answer() As Char
        Get
            Player2Answer = plyPlayer2.chrPlayerAnswer
        End Get
        Set(ByVal Value As Char)
            plyPlayer2.chrPlayerAnswer = Value
        End Set
    End Property

    Property Player3Answer() As Char
        Get
            Player3Answer = plyPlayer3.chrPlayerAnswer
        End Get
        Set(ByVal Value As Char)
            plyPlayer3.chrPlayerAnswer = Value
        End Set
    End Property

    ReadOnly Property Question() As String
        Get
            Question = crqActiveQuestion.strQuestion
        End Get
    End Property

    ReadOnly Property Answer1() As String
        Get
            Answer1 = crqActiveQuestion.strAnswer1
        End Get
    End Property

    ReadOnly Property Answer2() As String
        Get
            Answer2 = crqActiveQuestion.strAnswer2
        End Get
    End Property

    ReadOnly Property Answer3() As String
        Get
            Answer3 = crqActiveQuestion.strAnswer3
        End Get
    End Property

    ReadOnly Property Answer4() As String
        Get
            Answer4 = crqActiveQuestion.strAnswer4
        End Get
    End Property

    ReadOnly Property CorrectAnswer() As String
        Get
            CorrectAnswer = crqActiveQuestion.strCorrectAnswer
        End Get
    End Property

    ReadOnly Property QuestionUsed() As String
        Get
            QuestionUsed = crqActiveQuestion.strQuestionUsed
        End Get
    End Property
#End Region

#Region "Methods"

    Sub Player1Correct()
        plyPlayer1.intPlayerScore += 100
    End Sub

    Sub Player2Correct()
        plyPlayer2.intPlayerScore += 100
    End Sub

    Sub Player3Correct()
        plyPlayer3.intPlayerScore += 100
    End Sub

    Sub NextQuestion()

        intRandomQuestion = CInt(Int(6 * Rnd()))

        ''Checks if question has been asked, if not sets data to properties
        If strQandA(intRandomQuestion, 6) = "0" Then
            crqActiveQuestion.strQuestion = strQandA(intRandomQuestion, 0)
            crqActiveQuestion.strAnswer1 = strQandA(intRandomQuestion, 1)
            crqActiveQuestion.strAnswer2 = strQandA(intRandomQuestion, 2)
            crqActiveQuestion.strAnswer3 = strQandA(intRandomQuestion, 3)
            crqActiveQuestion.strAnswer4 = strQandA(intRandomQuestion, 4)
            crqActiveQuestion.strCorrectAnswer = strQandA(intRandomQuestion, 5)
            strQandA(intRandomQuestion, 6) = 1
        Else
            NextQuestion()
        End If

    End Sub

    Function GetPlayer1Name() As String

    End Function

    Function GetPlayer2Name() As String

    End Function

    Function GetPlayer3Name() As String

    End Function

    Sub CheckAnswers()
        If plyPlayer1.chrPlayerAnswer = strQandA(intRandomQuestion, 5) Then
            Player1Correct()
        End If

        If plyPlayer2.chrPlayerAnswer = strQandA(intRandomQuestion, 5) Then
            Player2Correct()
        End If

        If plyPlayer3.chrPlayerAnswer = strQandA(intRandomQuestion, 5) Then
            Player3Correct()
        End If
    End Sub


#End Region

End Class
 
Here is the ENTIRE class, but the main problem seems to be pointing to the rand feature in the "NextQuestion" sub routine. Thanks to anyone who spots it. I''m clueless myself.

Share this post


Link to post
Share on other sites
No, pretty sure it isn''t the install, it happens right around the random number catch. I''m not sure if I''m using it correctly. I need random number between 0-14 for choosing the question. It seems to be over flowing the stack and Im'' not sure why. Anyone know how to use the random number functions in VB well?

Share this post


Link to post
Share on other sites
Looks like in the method "NextQuestion" if all elements have been changed to 1 it will end up just calling itself recursively and cause the stack overflow - debug it and check it out...

Note that on the line "intRandomQuestion = CInt(Int(6 * Rnd()))
", the end output will be between 0 and 5 - rnd returns between 0 inclusive and 1 exclusive, so 6*rnd will give you values between 0 and 5.999(9) and int of that will yield between 0 and 5

"I woke up sweating and clutching my pillow. Thankfully the powerful and reassuring visage of Optimus Prime staring back at me from my pillow case served to ease my frayed nerves. Like the giant robotic father I never had he always knows just what to say" - Gabe, Penny-Arcade

Alexandre Moura

Share this post


Link to post
Share on other sites