CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Hybrid View

  1. #1
    Join Date
    Aug 2008
    Posts
    111

    Exclamation 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
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Asking questions in an array

    Code:
    If theQNum <> totalQs  Then
         currentQ = currentQ + 1
         lblQ.Text = theQuestions(currentQ)
    The problem is with your if statement. You have a 0 based array with 6 items within. The last item will be number 5 but your routine will try to set the label text to the value of TheQuestions(6) which does not exist.


    Code:
    If theQNum < totalQs -1 Then
                    currentQ = currentQ + 1
                    lblQ.Text = theQuestions(currentQ)

    btw you would be much better off using a loop rather than the repeated If tests. A loop will allow for any number of questions with less code than you are currently using.
    Last edited by DataMiser; January 11th, 2012 at 07:59 AM.
    Always use [code][/code] tags when posting code.

  3. #3
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Asking questions in an array

    Actually after looking closer a loop is not needed since you are calling the sub repeatedly from a button click. You also do not need all the if tests as the same code can be used to process all of the questions.

    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()
            Else
                theAnswers(currentQ) = "-" & txtNumber.Text
    
                If currentQ < totalQs-1 Then
                    currentQ = currentQ + 1
                    lblQ.Text = theQuestions(currentQ)
    
                    If (totalQs - currentQ) = 1 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
    Notice how much code I was able to remove and that you are no longer limited to just 6 questions
    Last edited by DataMiser; January 11th, 2012 at 08:10 AM.
    Always use [code][/code] tags when posting code.

  4. #4
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Asking questions in an array

    You could also use a LIST of Strings
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  5. #5
    Join Date
    Aug 2008
    Posts
    111

    Re: Asking questions in an array

    Quote Originally Posted by DataMiser View Post
    Actually after looking closer a loop is not needed since you are calling the sub repeatedly from a button click. You also do not need all the if tests as the same code can be used to process all of the questions.

    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()
            Else
                theAnswers(currentQ) = "-" & txtNumber.Text
    
                If currentQ < totalQs-1 Then
                    currentQ = currentQ + 1
                    lblQ.Text = theQuestions(currentQ)
    
                    If (totalQs - currentQ) = 1 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
    Notice how much code I was able to remove and that you are no longer limited to just 6 questions
    I took out most of the IF ELSE since it asks questions like if txtnumber.text = "" then... etc etc out of the code posted since it would have made it longer. I only have the If else because i check for stuff in the txtNumber box.

  6. #6
    Join Date
    Aug 2008
    Posts
    111

    Re: Asking questions in an array

    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

  7. #7
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Asking questions in an array

    That looks much better
    Always use [code][/code] tags when posting code.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured