Trouble sorting numbers in viewstate array

Emo Elmo

New member
Joined
Aug 28, 2008
Messages
2
Programming Experience
Beginner
The code will output whats put in the lap time textbox into the listbox but as soon as the button is pushed to put the next number in it over writes it instead of adding the second value. does anyone know what is wrong with my code?

VB.NET:
Partial Class _Default
    Inherits System.Web.UI.Page
    Private arrLaps(100) As Integer
    Private arrtime(100) As Decimal
    Private arrcounter As Integer

    Protected Sub addtime_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles addtime.Click
        Dim laptime As Decimal
        Dim lapnumber As Integer
        Dim tmpSize As Integer
        Dim tmp As Decimal
        Dim arrcounter As Integer
        Dim i As Integer



        'convert strings into appropariate datatypes
        Decimal.TryParse(txtlaptime.Text, laptime)
        Integer.TryParse(txtlapnumber.Text, lapnumber)


        'store in next available position in array
        arrLaps(arrcounter) = lapnumber
        arrtime(arrcounter) = laptime
        arrcounter = arrcounter + 1

        tmpSize = arrtime.Count - 1

        For Loop1 = 1 To tmpSize
            For Loop2 = 0 To tmpSize - Loop1
                If arrtime(Loop2) < arrtime(Loop2 + 1) Then
                    tmp = arrtime(Loop2)
                    arrtime(Loop2) = arrtime(Loop2 + 1)
                    arrtime(Loop2 + 1) = tmp
                End If
            Next Loop2
        Next Loop1

        'display contents of array by looping through elements in array and building a string of each item
        While i < arrcounter
            times.Items.Add(arrtime(i))
            i = (i + 1)
        End While
        'persist both array and counter in viewstate as all data is about to be destroyed (except that stored in ViewState)
        ViewState.Add("Laps", arrLaps)
        ViewState.Add("times", arrtime)
        ViewState.Add("counter", arrcounter)
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'this is the beginning of a new session so retrieve array and counter from viewstate
        If ViewState("Laps") IsNot Nothing Then      'on the first time through, nothing will have been
            arrLaps = ViewState("Laps")           'stored in viewstate
        End If
        If ViewState("times") IsNot Nothing Then
            arrtime = ViewState("times")
        End If
        If ViewState("counter") IsNot Nothing Then
            arrcounter = ViewState("counter")
        End If

    End Sub
End Class
 
Last edited by a moderator:
Back
Top