8 Replies - 1070 Views - Last Post: 29 January 2017 - 05:48 AM Rate Topic: -----

#1 alanSmirnov  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 26-January 17

Arrays and variables clears itself after a end sub

Posted 26 January 2017 - 04:52 PM

I am writing a times tables program in ASP.net and I need some help. The problem I am experiencing is that when the button sub ends and the screen pops up again for user input, all the arrays and variables are now set to nothing or 0.

Is there are way to keep all the values for variables after a sub ends and to use them later on?

Any help will be appreciated.


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


Is This A Good Question/Topic? 0
  • +

Replies To: Arrays and variables clears itself after a end sub

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 5078
  • View blog
  • Posts: 13,705
  • Joined: 18-April 07

Re: Arrays and variables clears itself after a end sub

Posted 26 January 2017 - 08:10 PM

Ok it sounds like you are new to ASP.net and probably had development of windows form projects before. You have to remember one thing about ASP.NET applications... they are in a stateless world. Meaning that you can set variables and such, and work with them, but only until the page is rendered and then the values are gone. Unless you store the variables in something like the session, cache or ViewState, they will not be carried over at all to the next rendering of the page.

So for instance, if I am setting a variable in a button click, when the click ends and the page is finished executing (and sent to the user), the server forgets all about that variable. If you even refresh the page, it will have to setup that variable again and reexecute all the previous stuff. But then again it will throw away the data.

Sometimes you need to keep values around and send them to other pages or some how carry the value into the next rendering of the page. Where the data is stored will determine where it can be read back in at. I suggest reading the following article which will tell you all about your options for passing data around. Keep in mind that while the article reads "...between pages" those pages could be the same page.

https://msdn.microso...y/6c3yckfw.aspx


Enjoy! :)
Was This Post Helpful? 2
  • +
  • -

#3 alanSmirnov  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 26-January 17

Re: Arrays and variables clears itself after a end sub

Posted 29 January 2017 - 03:24 AM

I am writing a times tables program in ASP.net and I need some help. The problem I am experiencing is i need help on how to increment a value each time a question has been answered and I have no idea how to do it in ASP.net, its very different to vb.net and the logical part. Another problem is that I want the back color to stay for 1 second and then move onto the next question but I have I was unsuccessful in achieving this.

Any help will be appreciated.

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
    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
        Session.Add("NumberofQuestions", NumberQ)
        Session.Add("FirstNumberArray", FirstNumber)
        Session.Add("SecondNumberArray", SecondNumber)
    End Sub
    Protected Sub txtInput_TextChanged(sender As Object, e As EventArgs) Handles txtInput.TextChanged
        Dim NewNumberQ As Integer = CType(Session.Item("NumberofQuestions"), Integer)
        Dim FirstNumberSession() As Integer = CType(Session.Item("FirstNumberArray"), Integer())
        Dim SecondNumberSession() As Integer = CType(Session.Item("SecondNumberArray"), Integer())
        inc = Request.QueryString("inc")
        If txtInput.Text = FirstNumberSession(inc) * SecondNumberSession(inc) Then
            correctAnswers = correctAnswers + 1
            txtInput.BackColor = Drawing.Color.Green
        Else
            txtInput.BackColor = Drawing.Color.Red
        End If
        System.Threading.Thread.Sleep(1000)
        PanelQuestion.Visible = True
    End Sub
    Public Sub NextQuestion()
        Dim FirstNumberSession() As Integer = CType(Session.Item("FirstNumberArray"), Integer())
        Dim SecondNumberSession() As Integer = CType(Session.Item("SecondNumberArray"), Integer())
        lblFirstN.Text = FirstNumberSession(inc + 1)
        lblSecondN.Text = SecondNumberSession(inc + 1)
        txtInput.Text = ""
        txtInput.BackColor = Drawing.Color.Empty
        PanelQuestion.Visible = True
    End Sub
End Class

Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is offline

  • say what now
  • member icon

Reputation: 6409
  • View blog
  • Posts: 25,903
  • Joined: 12-December 12

Re: Arrays and variables clears itself after a end sub

Posted 29 January 2017 - 03:56 AM

That is essentially the same code and question. Topics merged.
Was This Post Helpful? 0
  • +
  • -

#5 alanSmirnov  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 26-January 17

Re: Arrays and variables clears itself after a end sub

Posted 29 January 2017 - 04:20 AM

View Postandrewsw, on 29 January 2017 - 03:56 AM, said:

That is essentially the same code and question. Topics merged.


Sorry , this is just the first time using ASP.net , its quite different and new , I have searched over the internet and couldnt find a good explanation on how manage this problem.
Was This Post Helpful? 0
  • +
  • -

#6 alanSmirnov  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 26-January 17

Re: Arrays and variables clears itself after a end sub

Posted 29 January 2017 - 04:38 AM

View PostMartyr2, on 26 January 2017 - 08:10 PM, said:

Ok it sounds like you are new to ASP.net and probably had development of windows form projects before. You have to remember one thing about ASP.NET applications... they are in a stateless world. Meaning that you can set variables and such, and work with them, but only until the page is rendered and then the values are gone. Unless you store the variables in something like the session, cache or ViewState, they will not be carried over at all to the next rendering of the page.

So for instance, if I am setting a variable in a button click, when the click ends and the page is finished executing (and sent to the user), the server forgets all about that variable. If you even refresh the page, it will have to setup that variable again and reexecute all the previous stuff. But then again it will throw away the data.

Sometimes you need to keep values around and send them to other pages or some how carry the value into the next rendering of the page. Where the data is stored will determine where it can be read back in at. I suggest reading the following article which will tell you all about your options for passing data around. Keep in mind that while the article reads "...between pages" those pages could be the same page.

https://msdn.microso...y/6c3yckfw.aspx


Enjoy! :)/>

Thanks you very much
Was This Post Helpful? 0
  • +
  • -

#7 andrewsw  Icon User is offline

  • say what now
  • member icon

Reputation: 6409
  • View blog
  • Posts: 25,903
  • Joined: 12-December 12

Re: Arrays and variables clears itself after a end sub

Posted 29 January 2017 - 04:39 AM

Did you read the page that Martyr2 provided?

As this is your first time using ASP.NET then I recommend that you obtain a book on the subject and work through it, or complete a substantial introductory tutorial, which will walk you through the process including handling statelessness. Some topics, particularly frameworks, are too big to be picked up from jumping between internet pages.
Was This Post Helpful? 0
  • +
  • -

#8 alanSmirnov  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 26-January 17

Re: Arrays and variables clears itself after a end sub

Posted 29 January 2017 - 05:25 AM

This is a project which i decided in college (in the UK) to help me get more points for entry in the top universities and the whole project has to be completed in like 6 months including all paper work. I have worked on implementing a SQL Server Databases and creating a good login and register screen and now this is the last part. The program will not be run on the internet, the project is only a way of learning a skills. If i can make it a standalone working web application that will be enough. If you can give me some advice on the last part i will be very grateful.

This post has been edited by andrewsw: 29 January 2017 - 05:37 AM
Reason for edit:: Removed previous quote, just press REPLY

Was This Post Helpful? 0
  • +
  • -

#9 andrewsw  Icon User is offline

  • say what now
  • member icon

Reputation: 6409
  • View blog
  • Posts: 25,903
  • Joined: 12-December 12

Re: Arrays and variables clears itself after a end sub

Posted 29 January 2017 - 05:48 AM

I refer you back to the article that Martyr2 linked. Understanding how ASP.NET deals with the lack of state is essential to an understanding of ASP.NET itself.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1