#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    164
    Rep Power
    0

    Question Asking questions in an array


    Hey all i am having a hard time coming up with the correct flow for a question form. Lets say i have 6 questions. I define them like this:
    Code:
         Dim firstStart As Boolean = True
         Dim totalQs As Integer = 0
         Dim currentQ As Integer = 0
         Dim theAnswers() As String
         Dim theQuestions() As String = {"Please scan barcode 1 then press NEXT", "" & _
                                        "Please scan barcode 1 then press NEXT", "" & _
                                        "Please scan barcode 1 then press NEXT", "" & _
                                        "Please scan barcode 1 then press NEXT", "" & _
                                        "Please scan barcode 1 then press NEXT", "" & _
                                        "Please scan barcode 1 then press COMPLETE"}
    The Next/Complete button code looks like this:
    Code:
        Private Sub cmdNextFinish_Click(ByVal sender As System.Object, ByVal e As             System.EventArgs) Handles cmdNextFinish.Click
           Call theQs(currentQ)
        End Sub
    The form_load looks like this:
    Code:
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Call theQs(0)
        End Sub
    And now the question setup looks like this:
    Code:
        Private Sub theQs(ByRef theQNum As Integer)
            If firstStart = True And theQNum = 0 Then
                firstStart = False
                totalQs = (theQuestions.Length)
                ReDim theAnswers(totalQs)
                lblQ.Text = theQuestions(0)
                cmdNextFinish.Enabled = True
                cmdNextFinish.Text = "NEXT"
                Call buttons(theQNum)
                txtNumber.Text = ""
                txtNumber.MaxLength = theQuestionTextboxLimits(theQNum)
                txtNumber.Focus()
            ElseIf theQNum = 0 Then 'ANSWERING THE FIRST QUESTION
                theAnswers(currentQ) = "-" & txtNumber.Text
    
                If currentQ <> totalQs Then
                    currentQ = currentQ + 1
                    lblQ.Text = theQuestions(currentQ)
    
                    If (totalQs - currentQ) = 0 Then
                        cmdNextFinish.Text = "Complete"
                    Else
                        cmdNextFinish.Text = "NEXT"
                    End If
    
                    txtNumber.Text = ""
                    txtNumber.MaxLength = theQuestionTextboxLimits(theQNum)
                    txtNumber.Focus()
                    Call buttons(currentQ)
                Else
                    'Call writeXMLFile()
                    MsgBox("exited")
                End If
            ElseIf theQNum = 1 Then 'ANSWERING THE SECOND QUESTION
                theAnswers(currentQ) = txtNumber.Text
    
                If theQNum <> totalQs Then
                    currentQ = currentQ + 1
                    lblQ.Text = theQuestions(currentQ)
    
                    If (totalQs - currentQ) = 0 Then
                        cmdNextFinish.Text = "Complete"
                    Else
                        cmdNextFinish.Text = "NEXT"
                    End If
    
                    txtNumber.Text = ""
                    txtNumber.MaxLength = theQuestionTextboxLimits(theQNum)
                    txtNumber.Focus()
                    Call buttons(currentQ)
                Else
                    'Call writeXMLFile()
                    MsgBox("exited")
                End If
            ElseIf theQNum = 2 Then 'ANSWERING THE THIRD QUESTION
                theAnswers(currentQ) = txtNumber.Text
    
                If theQNum <> totalQs Then
                    currentQ = currentQ + 1
                    lblQ.Text = theQuestions(currentQ)
    
                    If (totalQs - currentQ) = 0 Then
                        cmdNextFinish.Text = "Complete"
                    Else
                        cmdNextFinish.Text = "NEXT"
                    End If
    
                    txtNumber.Text = ""
                    txtNumber.MaxLength = theQuestionTextboxLimits(theQNum)
                    txtNumber.Focus()
                    Call buttons(currentQ)
                Else
                    'Call writeXMLFile()
                    MsgBox("exited")
                End If
            ElseIf theQNum = 3 Then 'ANSWERING THE FORTH QUESTION
                theAnswers(currentQ) = txtNumber.Text
    
                If theQNum <> totalQs Then
                    currentQ = currentQ + 1
                    lblQ.Text = theQuestions(currentQ)
    
                    If (totalQs - currentQ) = 0 Then
                        cmdNextFinish.Text = "Complete"
                    Else
                        cmdNextFinish.Text = "NEXT"
                    End If
    
                    txtNumber.Text = ""
                    txtNumber.MaxLength = theQuestionTextboxLimits(theQNum)
                    txtNumber.Focus()
                    Call buttons(currentQ)
                Else
                    'Call writeXMLFile()
                    MsgBox("exited")
                End If
            ElseIf theQNum = 4 Then 'ANSWERING THE FIFTH QUESTION
                theAnswers(currentQ) = txtNumber.Text
    
                If theQNum <> totalQs Then
                    currentQ = currentQ + 1
                    lblQ.Text = theQuestions(currentQ)
    
                    If (totalQs - currentQ) = 0 Then
                        cmdNextFinish.Text = "Complete"
                    Else
                        cmdNextFinish.Text = "NEXT"
                    End If
    
                    txtNumber.Text = ""
                    txtNumber.MaxLength = theQuestionTextboxLimits(theQNum)
                    txtNumber.Focus()
                    Call buttons(currentQ)
                Else
                    'Call writeXMLFile()
                    MsgBox("exited")
                End If
            ElseIf theQNum = 5 Then 'ANSWERING THE SIXTH QUESTION
                theAnswers(currentQ) = txtNumber.Text
    
                If theQNum <> totalQs Then
                    currentQ = currentQ + 1
                    lblQ.Text = theQuestions(currentQ)
    
                    If (totalQs - currentQ) = 0 Then
                        cmdNextFinish.Text = "Complete"
                    Else
                        cmdNextFinish.Text = "NEXT"
                    End If
    
                    txtNumber.Text = ""
                    txtNumber.MaxLength = theQuestionTextboxLimits(theQNum)
                    txtNumber.Focus()
                    Call buttons(currentQ)
                Else
                    'Call writeXMLFile()
                    MsgBox("exited")
                End If
            End If
        End Sub
    I seem to be getting confused because when it gets to the 5th question it gives me an error of **Index was outside the bounds of the array.** on the line of

    Code:
    lblQ.Text = theQuestions(currentQ)
    for

    Code:
    theQNum = 5
    I know its on the 5th question but the array does not go up to 6.

    What am i doing wrong here (or overthinking something simple)

    If anyone has a better way of doing this then please let me know as this approach is not going so well! Ugg

    Thanks,

    David
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    164
    Rep Power
    0
    Got it:
    Code:
    Private theQNum As Integer
    
    Sub Start
       theQNum =0
       SetupNextQuestion
    End Sub
    
    Sub SetupNextQuestion
       txtNumber.Text = ""
       lblQuestion.Text = theQuestions(theQNum)
    
       If theQNum = (theQuestions.Length - 1) Then
            cmdNextFinish.Text = "Complete"
       Else
            cmdNextFinish.Text = "NEXT"
       End If
    
    End Sub
    
    Sub cmdNextFinish_Click
       theAnswers(theQNum) = txtNumber.Text
       
       'Check if this is a finish
       theQNum += 1
       If theQNum >= theQuestions.Length Then
            'Call writeXMLFile()
             MsgBox("exited")
       Else 
         SetupNextQuestion
       End If
    End Sub
    David

IMN logo majestic logo threadwatch logo seochat tools logo