• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 282
  • Last Modified:

VB.NET 2.0 Create Array for the session to be accessed from any page.

How to create a public array in VB.NET 2.0 for a web form aspx How do I create an array that I can access from any page. I want this global array to be accessible for the session.

Thank you for your help.
0
GDene
Asked:
GDene
  • 4
  • 4
  • 3
5 Solutions
 
Bob LearnedCommented:
If you want a variable to be active for a session only, then you can define a Session variable, and access from anywhere:

Set:
Session("name") = "Bob"

Get:
Dim name As String = Session("name")

If you need for every user to access the variable, then you would define an Application variable.

Bob
0
 
DropZoneCommented:
Create the array and store a reference to it in the Application State collection, which is global to all requests:

HttpContext.Current.Application("MyArray") = MyArray

You may want to lock the Application State collection prior to modifying it, to make sure that nobody else can access it until it is ready:

HttpContext.Current.Application.Lock
HttpContext.Current.Application("MyArray") = MyArray
HttpContext.Current.Application.UnLock

The Application State object is exposed by the Page control as a property, so you do not have to access it directly from the current context if you are going to do this from, say, the Page_Load event:

Sub Page_Load(Sender As Object)
    Application.Lock
    Application("MyArray") = MyArray
    Application.UnLock
End Sub

      -dZ.
0
 
GDeneAuthor Commented:
Thank you for the application array solution but my understanding is application will be there for all users to the site. I don't want users to share the array.
Each user must have an array for their own session that holds their responses until they decide they want it saved. Then I will write it to the database.
The session("name") approach is fine for a few items when you know how many you are going to need. The array will suit my purpose much better because it will adjust to size  needed.
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
DropZoneCommented:
You asked "How to create a public array" -- For that you use the Application state.  If you want to do this for each user, then of course you use the Session state.

I think you misunderstood TheLearnedOne's comment:  where he said "name" he meant the name or key for the Session state entry.  The Session state can hold an indefinite amount of items and you do not need to know in advance how many there will be.  You can use it to store the Array itself, not each item:

Sub Page_Load(Sender As Object)
    ' store the array in the Session state
    Session("MyArray") = MyArray
End Sub

I'm not very versed in VB.NET, so I'm not sure if you required the SET keyword to assign arrays and objects as you did in VB 6.  If so, then change the line to:
    Set Session("MyArray") = MyArray

     -dZ.
0
 
Bob LearnedCommented:
No, I am saying put the entire array into the session variable, not an individual item.  That was just an example of a storing a value in a variable.

Bob
0
 
DropZoneCommented:
The following phrase in my message above
    "You can use it to store the Array itself, not each item"

should have been:
  " You can use it to store the Array itself, not necessarily each item"

You can store the array, each item, or any object or value in the Session state, as well as the Application state.

    -dZ.
0
 
GDeneAuthor Commented:
Glad to hear I can use a session but still unclear of the syntax to add more to the array.
Lets start it off with one row of data in an array for 4 rows and 3 columns
Dim MyArray(2,3)
MyArray(0,0) = "Toyata"
MyArray(1,0) = "White"
MyArray(2,0) = "22,000"
Session("SessionArray") = MyArray
then how do I add a 2nd row to the Session("SessionArray") when the user gives me more data
how do I extract from the Session("SeesionArray") the 2nd column first row data?

thank you for your patience

0
 
Bob LearnedCommented:
With 2.0, there are different structures that would get the job done better:

1) Define a class with the different properties (Manufacturer, Color, GVWR, etc.)

2) Define a generic list to hold the instance of objects:

   Dim cars As New List(Of Car)
   Dim entry As New Car()
   entry.Manufacturer = "Toyota"
   entry.Color = "White"
   cars.Add(entry)

3) Add to session:

    Session("CarList") = list

4) Get list from session:

    list = CType(Session("CarList), List(Of Car))

Bob
0
 
GDeneAuthor Commented:
Thank you - your solution is interesting.
If I understand your solution correctly the session would hold the list for one row.
How would I add another row to that from another sub.

I'm looking for the flexibility I get from a "public /  global" array. Where I add things into the array from any sub procedure.  I can look at the contents of a particular cell in the array and then decide on that to display on the screen etc. I can keep adding to the array from any sub procedure.




0
 
DropZoneCommented:
You can access the array from the Session object directly:
    Session("CarList")(1) = "Toyota"

Or by getting a reference to it:
   MyArray = Session("CarList")
   MyArray(1) = "Toyota"

However, if I understand your comment correctly, what you want is to store a list of "car structures", not just an array of strings.  For this, perhaps is better to create a new class to store the car information and then add this into an array and store it in the Session state.

Perhaps it would help if you could explain with more details what you ultimately want to accomplish.

    -dZ.
0
 
Bob LearnedCommented:
The solution would store the entire list in the Session variable:

   Session("CarList") = list

You can add and remove items from the list a lot more easily with a list like this than an Array.

Bob
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

  • 4
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now