在数组中提出问题

时间:2012-01-11 07:49:43

标签: vb.net arrays arraylist

嘿,我很难想出一个问题表格的正确流程。可以说我有6个问题。我这样定义它们:

 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"}

Next / Complete按钮代码如下所示:

Private Sub cmdNextFinish_Click(ByVal sender As System.Object, ByVal e As             System.EventArgs) Handles cmdNextFinish.Click
   Call theQs(currentQ)
End Sub

form_load看起来像这样:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Call theQs(0)
End Sub

现在问题设置如下:

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

我似乎感到困惑,因为当它到达第5个问题时,它给出了索引超出数组范围的错误。

行上
lblQ.Text = theQuestions(currentQ)

theQNum = 5

我知道它在第5个问题但是数组不会达到6。

我在这里做错了什么(或者过度简单的事情)

谢谢,

大卫

更新GOT IT

 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

大卫

2 个答案:

答案 0 :(得分:1)

这些行:

If theQNum <> totalQs Then
            currentQ = currentQ + 1
            lblQ.Text = theQuestions(currentQ)

If for your ElseIf theQNum = 5 Then中的哪些内容会导致您超出范围。但问题是你的问题早得多。你从

开始
If firstStart = True And theQNum = 0 Then
       ' Code here
ElseIf theQNum = 0 Then 'ANSWERING THE FIRST QUESTION
       'More code here that doesn't get called

如果在第一次运行时不会调用QNum = 0,则因为if的第一部分为true。当firstStart为true时,在第一位代码中,你不会增加currentQ,所以下次调用firststart时,firststart为false,但currentQ仍为0.问题的根源。

简单的解决方法是在if的第一位内增加currentQ并始终设置标签文本 BEFORE 增加currentQ。当你在最后一个问题时,不要增加它。这样currentQ永远不会超出范围。

答案 1 :(得分:0)

数组从0开始。那么,你的第一个问题是在索引0&amp;第六个问题是数组中的索引5。如果事先为最后一个问题增加索引,则索引变为6,没有值。

相关问题