Home > Software design >  List.Add is replacing index 0 instead of adding to the next position. Why?
List.Add is replacing index 0 instead of adding to the next position. Why?

Time:11-13

I'm using ASP.NET and Visual Basic to make a custom form, used for filling in information. Once I click a button, a function gets called that takes the bits information from that form, puts them into an object, and adds that object to a list. This is being used for a sort of queued entry system, so the form will be edited and submitted multiple times.

For some reason, instead of adding the new object in the next index position of the list, it instead replaces whatever was at 0. So, there is only ever one object in the list at the time.

Here's the custom form: enter image description here

Here's my custom object, which is currently placed above my _Default class:

Public Class QueueItem

    Public Property _TestName As String
    Public Property _ValueID As String
    Public Property _MathOperator As String
    Public Property _ValueInput As Integer

    Public Sub New()
    End Sub

    Public Sub New(ByVal TestName As String, ByVal ValueID As String, ByVal MathOperator As String, ByVal ValueInput As Integer)
        _TestName = TestName
        _ValueID = ValueID
        _MathOperator = MathOperator
        _ValueInput = ValueInput
    End Sub

End Class

The list is declared above my Page_Load function, inside the _Default class, and is public. Here's that list definition:

Public QueueList As List(Of QueueItem) = New List(Of QueueItem)()

And, here's what gets called when that "Add To Queue" button is pressed:

Protected Sub AddToQueueButton_Click(sender As Object, e As EventArgs) Handles AddToQueueButton.Click

        'Adds a new QueueItem to the QueueList
        'Values pulled from the dropdown lists in the custom form

        QueueList.Add(New QueueItem() With {
            ._TestName = TestName.SelectedValue,
            ._ValueID = ValueID.SelectedValue,
            ._MathOperator = MathOperator.SelectedValue,
            ._ValueInput = ValueInput.Text
        })

        'Below section is for testing

        Dim test1 As String = QueueList(0)._TestName
        Dim test2 As String = QueueList(0)._ValueID
        Dim test3 As String = QueueList(0)._MathOperator
        Dim test4 As String = QueueList(0)._ValueInput

        Dim testmessage As String = test1   " | "   test2   " | "   test3   " | "   test4
        Dim count = QueueList.Count
        Dim capacity = QueueList.Capacity

        Response.Write("<script language='javascript'>alert('"   testmessage   "');</script>")

End Sub

So, as you can see, I have some test variables and stuff that I'm using to make sure this is working. Any time this gets called, an object gets added to the list, I look at the count and capacity for the list, and I display all the information in an alert.

This information for the alert is always reading from index 0. So, it shouldn't matter how many times I add information to the list, 0 should stay the same, and objects should be added at 1, then 2, and so on. Right?

Well, 0 changes any time I submit new information, and neither the count or capacity never increase past the first entry. They always display as if there's only one item in the list.

Here's me running the queue entry form twice, with two different numbers on the end:

First run:

enter image description here enter image description here

Second run:

enter image description here enter image description here

Since I'm always reading from index 0, that number at the end shouldn't change. It should be giving me the number that's associated with the object at index 0. And, the List.Add function should make the count and capacity go up. But, none of that happens. Instead, it seems to be replacing what was at 0.

If anyone has any tips on how to fix this or can clue me in on what might be going on, that would be greatly appreciated.

CodePudding user response:

Every time a request is made to your page, a new instance of your class is created to handle the request. For a post-back request, some data may be loaded - for example, the values of the fields posted to the server, and anything stored in ViewState. But data stored in fields within your page will not be persisted across requests.

The code is not replacing the item at index 0; it is adding an item to an empty list.

You need to persist your list between requests somehow. You can either store it in the Session, or put it in the ViewState.

  • Related