在VB.net中的end sub之后,数组和变量会自行清除

时间:2017-01-26 22:28:13

标签: asp.net vb.net

我正在ASP.net中编写一个时间表程序,我需要一些帮助。我遇到的问题是,当按钮子结束并且屏幕再次弹出以供用户输入时,所有数组和变量现在都设置为空或0。

是否有办法在子结束后保留​​变量的所有值并在以后使用它们?

任何帮助将不胜感激。

Public Class PickTimesTables
    Inherits System.Web.UI.Page

    Dim count As Integer = 0
    Dim NumberQ As Integer
    Dim RandomN As Integer
    Dim FirstNumber() As Integer
    Dim FirstNumberTemp() As Integer
    Dim SecondNumber() As Integer
    Dim correctAnswers As Integer
    Dim inc As Integer = 0

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        PanelQuestion.Visible = False
        PanelAnalysis.Visible = False
    End Sub

    Protected Sub btnTest_Click(sender As Object, e As EventArgs) Handles btnTest.Click
        PanelQuestion.Visible = True
        lblOperator.Text = "X"
        If chktimes1.Checked = False And chktimes2.Checked = False And chktimes3.Checked = False And chktimes4.Checked = False And chktimes5.Checked = False And chktimes6.Checked = False And chktimes7.Checked = False And chktimes8.Checked = False And chktimes9.Checked = False And chktimes10.Checked = False And chktimes11.Checked = False And chktimes12.Checked = False Then
            MsgBox("Pick a Times Table to be Tested for..")
        ElseIf txtNoQuestion.Text = "" Then
            MsgBox("Pick the Number of Question for the test")
        Else
            NumberQ = txtNoQuestion.Text
            count = 0
            If chktimes1.Checked Then
                count = count + 1
            End If
            If chktimes2.Checked Then
                count = count + 1
            End If
            If chktimes3.Checked Then
                count = count + 1
            End If
            If chktimes4.Checked Then
                count = count + 1
            End If
            If chktimes5.Checked Then
                count = count + 1
            End If
            If chktimes6.Checked Then
                count = count + 1
            End If
            If chktimes7.Checked Then
                count = count + 1
            End If
            If chktimes8.Checked Then
                count = count + 1
            End If
            If chktimes9.Checked Then
                count = count + 1
            End If
            If chktimes10.Checked Then
                count = count + 1
            End If
            If chktimes11.Checked Then
                count = count + 1
            End If
            If chktimes12.Checked Then
                count = count + 1
            End If

            If txtNoQuestion.Text = 0 Then
                MsgBox("You cannot have a test with 0 Questions")
            Else
                ReDim FirstNumberTemp(count - 1)
                For i = 0 To count - 1
                    If chktimes1.Checked Then
                        FirstNumberTemp(i) = 1
                        chktimes1.Checked = False
                    ElseIf chktimes2.Checked Then
                        FirstNumberTemp(i) = 2
                        chktimes2.Checked = False
                    ElseIf chktimes3.Checked Then
                        FirstNumberTemp(i) = 3
                        chktimes3.Checked = False
                    ElseIf chktimes4.Checked Then
                        FirstNumberTemp(i) = 4
                        chktimes4.Checked = False
                    ElseIf chktimes5.Checked Then
                        FirstNumberTemp(i) = 5
                        chktimes5.Checked = False
                    ElseIf chktimes6.Checked Then
                        FirstNumberTemp(i) = 6
                        chktimes6.Checked = False
                    ElseIf chktimes7.Checked Then
                        FirstNumberTemp(i) = 7
                        chktimes7.Checked = False
                    ElseIf chktimes8.Checked Then
                        FirstNumberTemp(i) = 8
                        chktimes8.Checked = False
                    ElseIf chktimes9.Checked Then
                        FirstNumberTemp(i) = 9
                        chktimes9.Checked = False
                    ElseIf chktimes10.Checked Then
                        FirstNumberTemp(i) = 10
                        chktimes10.Checked = False
                    ElseIf chktimes11.Checked Then
                        FirstNumberTemp(i) = 11
                        chktimes11.Checked = False
                    ElseIf chktimes12.Checked Then
                        FirstNumberTemp(i) = 12
                        chktimes12.Checked = False
                    End If
                Next
                ReDim FirstNumber(NumberQ - 1)
                For i = 0 To NumberQ - 1
                    Randomize()
                    RandomN = FirstNumberTemp(Int(Rnd() * count))
                    FirstNumber(i) = RandomN
                Next

                ReDim SecondNumber(NumberQ - 1)
                For i = 0 To NumberQ - 1
                    Dim rn As New Random(Now.Millisecond)
                    RandomN = rn.Next(1, 13)
                    SecondNumber(i) = RandomN
                Next

                lblFirstN.Text = FirstNumber(0)
                lblSecondN.Text = SecondNumber(0)
            End If
        End If
    End Sub

    Protected Sub txtInput_TextChanged(sender As Object, e As EventArgs) Handles txtInput.TextChanged
        NumberQ = txtNoQuestion.Text
        If txtInput.Text = FirstNumber(inc) * SecondNumber(inc) Then
            lblFirstN.Text = FirstNumber(inc + 1)
            lblSecondN.Text = SecondNumber(inc + 1)
            correctAnswers = correctAnswers + 1
            txtInput.BackColor = Drawing.Color.Green
        Else
            txtInput.BackColor = Drawing.Color.Red
            lblFirstN.Text = FirstNumber(inc + 1)
            lblSecondN.Text = SecondNumber(inc + 1)
        End If
    End Sub
End Class

2 个答案:

答案 0 :(得分:0)

他们都会被重置,因为这是网络的无国籍性质。他们不会保留自己的价值,因为每次您查看该页面(或使用调试器逐步执行该页面)时,都会有新的请求。

您应该在ViewState或Session中存储asp.net变量,即ViewState支持的属性。

答案 1 :(得分:0)

对于ASP.Net Webforms的新手来说,这是一个常见的错误。有这样的想法,您的页面类实例适用于整个会话,用于所有用户与浏览器中页面的交互。 这种情况不会发生。与您的网页进行任何导致事件触发的交互都会创建从客户端到服务器的新回发,每次回发都会使用Page class <的全新实例< / em>的。当用户在浏览器中看到您的页面时,您用于呈现该视图的html的任何Page对象都已被销毁并被垃圾收集。

您可以采取一些措施来解决这个问题,例如将变量放在Session,URL或ViewState中,但通常需要完全重新考虑您对网站更自然的方法。< / p>

此外,您还需要重新考虑在VB.Net代码中处理TextChanged事件。一般来说,TextChanged事件需要在25-40毫秒内返回给用户,否则视图会感觉非常迟钝。当程序在用户的计算机上运行时,通常不会出现问题,并且该工作站的所有内存和CPU功率都可用。对于网站来说,这是另一个故事,在请求甚至到达服务器之前可能有80ms的延迟,响应的另外80ms,所有这些都在进行任何处理之前,服务器正在共享它的CPU和内存资源以支持尽可能多的用户可以适应。

相反,请考虑使用javascript事件进行该处理,并仅在用户最终尝试提交或保存最终结果时验证VB.Net代码中的文本。

最后,对MsgBox()函数的调用也需要进行。它似乎只适用于您的系统,因为您的Web服务器与Web浏览器位于同一台计算机上。当您实际将其放在真实服务器上并从其他地方进行测试时,这些消息将显示在服务器上,用户的Web浏览器中