Reputation: 6169
The way I would do it in Python is
Session["ShoppingCart"] = {}
Then if I added a new item to the cart and saved it, I would do it like so:
# Python/Flask
item = "Apple"
price = 2
quantity = 1
# Check it's not already in there...
if item not in Session["ShoppingCart"]:
Session["ShoppingCart"][item] = {"price": price, "quantity": quantity}
else:
# If it already exists, then increment the quantity
Session["ShoppingCart"][item]["quantity"] += 1
How would I do that same sort of flow in VB.NET for an ASPX page? What've come up with so far
'VB.NET'
Partial Class ShoppingCart
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Session("ShoppingCart") Is Nothing Then
Session("ShoppingCart") = New ArrayList
End If
Dim title As String = Request.QueryString("Title")
Dim price As String = Request.QueryString("Price")
Session("ShoppingCart").Add(title)
End Sub
End Class
As you can see I only know how to add one item at a time, instead of a dictionary/object like item.
However I'm not sure how to create a dictionary with VB.NET, my eventual outcome is to then display the Session shopping cart in the UI using GridView.
Upvotes: 0
Views: 324
Reputation: 74605
An arraylist is not a dictionary, so by storing an arraylist in the Session you're limiting yourself to working with an arraylist. You could at least store ShoppingCartItems in your arraylist and then you can access and index them.
Arraylist is very old, and i don't really use it any more since Generic collections came along. I can't see any use for ArrayList when one can have eg a List(Of Person) - both will store an expanding list of Person instances but the List(Of Person) will give things back to you as a Person whereas an arraylist returns them as objects, which requires a cast to Person
If you want to make this properly object oriented perhaps you should put an instance of a shopping cart class (you already have a shopping cart that inherits page but I'm hence thinking that this class is for showing the shopping cart contents) in your session. The cart can have a list of items, track coupons, keep the rolling total etc:
Class ShoppingCart
Public Property Items as List(Of ShoppingCartItem)
Public Property Coupons as List(Of Coupon)
Public ReadOnly Property Total as Double
'Every time an item is added to items or coupons, recalc total
End Class
Class ShoppingCartItem
'Put properties here
End class
'And then in your page (rename it to ViewCart so it doesn't clash or put it in a different namespace)
Session("cart") = new ShoppingCart
You may exclaim "but that's so much work compared to python!" - well, yeah; that's partly why .net languages are viewed as more grown up than scripty, loosely typed, relatively more rule-free languages. You can use anonymous types in .net so you don't have to formally declare classes etc but there is a value in declaring an object/domain hierarchy and working with it because it makes the modelling of the application data much more rigorous and forces you to think about things in greater depth/intensity rather than just "I need an extra string x in here, I'll just sling it in there.." which is what you get when you work with a language where things are "defined on the fly"
Upvotes: 3