asp classic:检查数组中的值

时间:2012-08-21 15:01:54

标签: asp-classic vbscript

我正在为Classic ASP中的客户制作一份简单的问卷。

这个想法是会有10个问题。用户注册并被发送到第一个问题。回答这个问题后,他们继续讨论2 nd 等问题。 可以跳过问题并在以后返回,每个问题只能回答一次。

我在用户已回答的每个问题的数据库中都有一个以逗号分隔的列表。

因此,用户登录并创建一个包含已回答问题列表的数组。

循环浏览此列表并转到第一个未回答的问题的最佳方法是什么?

回答问题的一个例子看起来像这样的“1,4,6” 所以这个用户会回答1号,4号和6号的问题。当用户登录时,我想将他们引导到第一个未答复的问题,在这种情况下2.一旦第二个问题得到解答,用户将被重定向到下一个悬而未决的问题。

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

@Dog,我认为这提供了您正在寻找的功能。

提示:有关将Microsoft的权威WSH参考作为Windows帮助文件下载的信息,请参阅this answer

Option Explicit

Dim oQsm : Set oQsm = New QuestionStatusManager

With oQsm
    .NumberOfQuestions = 10
    .RestoreStatus("1,4,6")
    .MarkQuestionAnswered(2)
    WScript.Echo "Questions " & .ToString() & " have been answered."
    WScript.Echo "Next unanswered question is: " & .GetNextUnansweredQuestion()
End With

Set oQsm = Nothing

' ------------------------------------------------------------------------

Class QuestionStatusManager

Private m_nNumberOfQuestions
Private m_aQuestionList()

Sub Class_Initialize()
    m_nNumberOfQuestions = -1
End Sub

Sub Class_Terminate()
    Erase m_aQuestionList
End Sub

Public Property Let NumberOfQuestions(n)
    Dim bValid : bValid = False

    If IsNumeric(n) Then
        If n = CInt(n) Then
            bValid = True
        End If
    End If

    If Not bValid Then
        Err.Raise vbObjectError + 1, "", _
           "Value '" & n & "' is not an integer."
    End If

    m_nNumberOfQuestions = CInt(n)
    ReDim m_aQuestionList(n)
End Property

Public Property Get NumberOfQuestions()
    CheckState
    NumberOfQuestions = m_nNumberOfQuestions
End Property

Private Sub CheckState()
    If m_nNumberOfQuestions = -1 Then
        Err.Raise vbObjectError + 1, "", _
           "Property 'NumberOfQuestions' has not been set."
    End If
End Sub

Sub RestoreStatus(sAlreadyAnswered)
    CheckState

    Dim aAlreadyAnswered : aAlreadyAnswered = Split(sAlreadyAnswered, ",")
    Dim i

    For i = 0 To UBound(m_aQuestionList)
        m_aQuestionList(i) = False
    Next

    For i = 0 To UBound(aAlreadyAnswered)
        m_aQuestionList(CInt(aAlreadyAnswered(i))) = True
    Next
End Sub

Sub MarkQuestionAnswered(n)
    Dim sDesc

    CheckState

    On Error Resume Next
    m_aQuestionList(n) = True

    If Err Or n = 0 Then
        sDesc = Err.Description
        On Error GoTo 0
        Err.Raise vbObjectError + 1, "", _
           "Can't mark question number '" & n & "' as answered: " & sDesc
    End If
End Sub

Function GetNextUnansweredQuestion()
    CheckState

    Dim i

    For i = 1 To UBound(m_aQuestionList)
        If Not m_aQuestionList(i) Then
            GetNextUnansweredQuestion = i
            Exit Function
        End If
    Next

    GetNextUnansweredQuestion = -1
End Function

Function ToString()
    CheckState

    Dim sDelim : sDelim = ""
    Dim i

    ToString = ""
    For i = 1 To UBound(m_aQuestionList)
        If m_aQuestionList(i) Then
            ToString = ToString & sDelim & CStr(i)
            sDelim = ","
        End If
    Next
End Function

End Class