user2120280
user2120280

Reputation: 25

how do i initialize my arraylist

I have a function that adds items to my arraylist. my problem is that it only holds one item at a time since it is reinitializing the array lit every time I click my button. what is the syntax in VB to only initialize the array if it has not been created yet?

 Dim itemSelectAs New ArrayList()
 Dim Quantities As New ArrayList()
 Dim itemQtyOrdered As Integer

Public Sub ShtickDataList_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.ListViewCommandEventArgs) Handles ShtickDataList.ItemCommand


    If e.CommandName = "addToCart" Then
        Dim itemQuantity As DropDownList = e.Item.FindControl("QuantityDropDown")
        itemQtyOrdered = itemQuantity.SelectedValue
        ItemSelect.Add(e.CommandArgument)
        Quantities.Add(itemQtyOrdered)

        Session("itemInCart") = ItemSelect
        Session("quantities") = Quantities


        viewInvoice()

    End If


End Sub

Protected Sub viewInvoice()

    Dim itemSelected As ArrayList = DirectCast(Session("itemInCart"), ArrayList)
    Dim QuantityofItem As ArrayList = DirectCast(Session("quantities"), ArrayList)

    Dim conn As SqlConnection
    Dim comm As SqlCommand
    Dim reader As SqlDataReader
    Dim purimConnection2 As String = ConfigurationManager.ConnectionStrings("Purim").ConnectionString
    conn = New SqlConnection(purimConnection2)

    comm = New SqlCommand("SELECT ProductName FROM Products WHERE ProductID = @ProductID", conn)

    Dim i As Integer
    For i = 0 To ItemSelect.Count - 1
    comm.Parameters.Add("@ProductID", Data.SqlDbType.Int)
    comm.Parameters("@ProductID").Value = (ItemSelected.Count - 1)

    'Next

    Try
        conn.Open()
        reader = comm.ExecuteReader()
        ViewCartlink.Text = "View Cart: (" & ItemSelected.Count & ")"



    Finally
        conn.Close()
    End Try
End Sub

Upvotes: 1

Views: 4976

Answers (2)

wax eagle
wax eagle

Reputation: 541

First you need to dimension your array list.

 Dim array_list as ArrayList()

Then you can instantiate one

 array_list = new ArrayList

Or you can combine it into one step:

 Dim array_list = new ArrayList()

After that you can add and remove elements from your array list with

 array_list.add(obj)

and remove with

 array_list.remove(obj)

It looks like your problem is related to accessing the members of an arraylist. New items are always added to the end of an arraylist. To access them directly, you will need their index. If you know the index of the item you want to access use

 array_list(i)

If you don't you will need to iterate over the array. To do this you have two options. You can use "for each" or you can use a normal for loop and use array_list.count as your upper bound.

You're recreating your two session values every time you call your button click menu. You need to pull them out of the Session variable and put them in local variables and put them back into the session variable.

Your button method should be:

 Public Sub ShtickDataList_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.ListViewCommandEventArgs) Handles ShtickDataList.ItemCommand


   if isNothing(itemSelect) Then itemSelect = New ArrayList()
   if isNothing(itemQtyOrdered) Then itemQtyOrdered= New ArrayList()

   If e.CommandName = "addToCart" Then


       Dim itemQuantity As DropDownList = e.Item.FindControl("QuantityDropDown")
       itemQtyOrdered = itemQuantity.SelectedValue
       ItemSelect.Add(e.CommandArgument)
       Quantities.Add(itemQtyOrdered)

       Session("itemInCart") = ItemSelect
       Session("quantities") = Quantities


       viewInvoice()

   End If


 End Sub

And change your Global calls to:

 Dim itemSelect As ArrayList() = Session("itemInCart")
 Dim Quantities As New ArrayList() = Session("quantities")

Upvotes: 3

Jay
Jay

Reputation: 6017

Define your array outside the button click event. (Form level)

Then in the button click event try this:

If myArrayList Is Nothing then
   'initializes the array list only if that hasn't happened yet
   myArrayList = new ArrayList
End If
'adds the item to the existing list without causing it to reintialize
myArrayList.add(item)

That way it is initialized if it hasn't been, but not if it already has. If it is initialized at the form level ie... its declared as new already then you can just add to it.

Basically make sure that you aren't calling New for the arrayList in the button click event.

Editing for web form:

You should probably check where you initialize your arrayList. Like in the Page_Load:

If Not Page.IsPostBack Then
  myArrayList = New ArrayList
End If

MSDN Postback

Upvotes: 2

Related Questions