Question MsgBox interfering with array

Magemaster

New member
Joined
Aug 6, 2009
Messages
3
Programming Experience
1-3
Hi,

I've built a quiz program that uses several arrays to store questions and answers. For debugging purposes I've inserted several msgboxes, when I remove them I get the same question in all of the question slots(same with answers) I spent a lengthy amount of time googling, and decided I needed to post.

Form2_load calls generateQuestion. If the msgbox is inside generateQuestion, the program works, if it is right after generateQuestion it works.

Code
VB.NET:
 Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim x As Integer = 0
        Dim y As Integer = 0
        ReDim Questions(CInt(Home.txtNoQ.Text))
        ReDim Answers(CInt(Home.txtNoQ.Text))
        ReDim Presidents(CInt(Home.txtNoQ.Text))

        lblQuestion2.Visible = False
        txtAnswer2.Visible = False
        lblQuestion3.Visible = False
        txtAnswer3.Visible = False
        lblQuestion1.Visible = False
        txtAnswer1.Visible = False
        btnSkip.Visible = False
        bSubmit.Visible = False
        btnYes.Visible = False
        ' btnYes.enalbed = False
        btnNo.Visible = False
        lblResults.Visible = False
        'MsgBox("beginning form2 activation")



        ' MsgBox("begining the loop") ''filling the question array
        While (x < CInt(Home.txtNoQ.Text))
            y = generateRandom(1, 5) 'generate the topic
            ''check the topic
            While (Home.questions(y) = 0)
                y = generateRandom(1, 5)
            End While
            'fill the array with questions
            generateQuestion(x, y)
            MsgBox(CStr(x) & " " & CStr(y)) ''if msgbox is here the program works
            x = x + 1
        End While
        lblQuestion1.Visible = True
        txtAnswer1.Visible = True
        btnSkip.Visible = True
        bSubmit.Visible = True
        Begin_Quiz()
        'Home = Nothing

        lblQuestion1.Visible = False
        txtAnswer1.Visible = False
        lblQuestion2.Visible = False
        txtAnswer2.Visible = False
        lblQuestion3.Visible = False
        txtAnswer3.Visible = False
        btnSkip.Visible = False
        bSubmit.Visible = False
        PictureBox1.Visible = False
        btnYes.Visible = True
        btnNo.Visible = True
        lblResults.Visible = True
    End Sub
VB.NET:
 Private Sub generateQuestion(ByVal index As Integer, ByVal topic As Integer)
        Dim pname As String
        president = generateRandom(Home.txtBegin.Text, Home.txtEnd.Text)
        pname = Home.ds.Tables("presidents").Rows(president).Item(1)
        Presidents(index) = president
        Select Case topic
            Case 1
                Questions(index) = "Who is this?"
                Answers(index) = pname
            Case 2
                Questions(index) = "What year was " + pname + " elected?"
                Answers(index) = Home.ds.Tables("presidents").Rows(president).Item(3)
            Case 3
                Questions(index) = "How many terms did " + pname + " serve?"
                If (IsDBNull(Home.ds.Tables("presidents").Rows(president).Item(5))) Then
                    Answers(index) = 1
                Else
                    Answers(index) = 2
                End If
            Case 4
                If (IsDBNull(Home.ds.Tables("presidents").Rows(president).Item(5))) Then
                    Questions(index) = "Who was " + pname + "'s Vice President?"
                    Answers(index) = Home.ds.Tables("presidents").Rows(president).Item(4)
                Else
                    Questions(index) = "Who were " + pname + "'s Vice Presidents?"
                    Answers(index) = Home.ds.Tables("presidents").Rows(president).Item(4) + " and " + Home.ds.Tables("presidents").Rows(president).Item(6)
                End If
            Case 5
                Questions(index) = "What party was " + pname + " in when he was elected?"
                Answers(index) = Home.ds.Tables("presidents").Rows(president).Item(2)
        End Select
        ' Home.Label1.Text = (Questions(index))
    End Sub

Thanks in advance.
 
More Information

By printing to some files, I figured out that the random number generator wasn't functioning, and x= x+1 wasn't working with out the msgbox.
random generator code:

VB.NET:
Private Function generateRandom(ByVal iLowerBound As Integer, ByVal iUpperBound As Integer) As Integer
        Dim z As Integer
        Randomize()
        Dim r As New Random
        z = r.Next(iLowerBound, iUpperBound)
        writer.WriteLine("Random: " & CStr(z))
        Return z
    End Function

Does a msgbox do anything to the program in terms of resetting anything?
 
By printing to some files, I figured out that the random number generator wasn't functioning, and x= x+1 wasn't working with out the msgbox.
random generator code:

VB.NET:
Private Function generateRandom(ByVal iLowerBound As Integer, ByVal iUpperBound As Integer) As Integer
        Dim z As Integer
        Randomize()
        Dim r As New Random
        z = r.Next(iLowerBound, iUpperBound)
        writer.WriteLine("Random: " & CStr(z))
        Return z
    End Function

Does a msgbox do anything to the program in terms of resetting anything?
MsgBox (should be using the .Net MessageBox.Show()) only haults the program on that line until it's acknowledged.

x += 1 (same thing as x = x + 1, just less typing) always works provided x is a numeric datatype (Byte, Short, Int, Long, Dec, Sng, Dbl).

As for your Random stuff not working, I don't know, but I do see you have a call to Randomize() which you don't need.
 
Thanks for the help

I wasn't sure if x+=1 worked in VB or not, I never tried it out (C is my primary).
I got rid of the whole randomGenerator() and just used a global x as random to do my random generation. I never called Randomize, though. Now that you pointed it out, I think that could've been my problem.
 
Back
Top