View Single Post
  #6 (permalink)  
Old July 1st, 2009, 02:29 PM
Imar's Avatar
Imar Imar is offline
Wrox Author
Points: 33,563, Level: 80
Points: 33,563, Level: 80 Points: 33,563, Level: 80 Points: 33,563, Level: 80
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 10,231
Thanks: 7
Thanked 203 Times in 201 Posts
Default

Ah, that explains it. Take a look at this:

Code:
Public Shared Quantities(500, 4) AsInteger' 0:index
A Shared variable exists on the class where you define it. At any tme, there's only one instance of that property throughout the entire application domain. In other words: all users have access to the same variable. If user A adds an item and user B queries the Quantities property, she'll see stuff added by user A. This is a bad design choice, as you won't be able to differentiate among users anymore. Now, take another look at my ShoopingCart property:

Code:
 
Public Shared ReadOnly Property ShoppingCart() As ShoppingCart
  Get
    If HttpContext.Current.Session("ShoppingCart") Is Nothing Then
      HttpContext.Current.Session("ShoppingCart") = New ShoppingCart()
    End If
    Return CType(HttpContext.Current.Session("ShoppingCart"), ShoppingCart)
  End Get
End Property
Althrough the property itself is marked as shared, its backing store is user scoped. This gives each user a unique shopping cart which isn't shared by others.

Hope this helps,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004

Did this post help you? Click the button to show your appreciation!
Reply With Quote