Confusion about shared vs. public vs. ?

  • Thread starter Thread starter Jeff
  • Start date Start date
J

Jeff

....still new to vb.net and vs 2005 web applications.

I remain confused about the "shared" variable/table designation and the
difference between "public" etc. I wish to place an entire table from a DB
into a datatable and then use it in a variety of different subs not unlike
the code below. I'm not positive, but it seems that "shared" might also
share the table across users, and this is not what I want. I only wish to be
able to access the table from multiple subs without running code to read it
from the DB in each sub. (but it will be a different table for each user).
Perhaps I need "public" or similar instead?

What should I be doing here for my purpose?
Thanks
Jeff


Shared MyTable As New DataTable
Protected Sub Page_Load ...

If Not Page.IsPostBack Then

MyTable = g.GetTable()

end if



Sub whatever()

MyTable.Rows(Session("Var1"))(Session("Var2"))

End sub

Sub whatever2()

MyTable.Rows(Session("Var3"))(Session("Var4"))

End sub
 
Jeff said:
...still new to vb.net and vs 2005 web applications.

I remain confused about the "shared" variable/table designation and the
difference between "public" etc. I wish to place an entire table from a DB
into a datatable and then use it in a variety of different subs not unlike
the code below. I'm not positive, but it seems that "shared" might also
share the table across users, and this is not what I want. I only wish to be
able to access the table from multiple subs without running code to read it
from the DB in each sub. (but it will be a different table for each user).
Perhaps I need "public" or similar instead?

What should I be doing here for my purpose?
Thanks
Jeff


Shared MyTable As New DataTable
Protected Sub Page_Load ...

If Not Page.IsPostBack Then

MyTable = g.GetTable()

end if



Sub whatever()

MyTable.Rows(Session("Var1"))(Session("Var2"))

End sub

Sub whatever2()

MyTable.Rows(Session("Var3"))(Session("Var4"))

End sub

Just as you suspected, making it Shared (static) will make all threads
use the same variable.

Notice that I said "threads", not "users". Different threads handle
different requests, but a thread is not assigned to handle requests from
a specific user, so you can't maintain user data based on threads.

What you are looking for is neither shared nor public. The variable can
be private and still be available for all methods within your Page class.

If you want to retain the data from one request to another, you can
store it in a session variable. Be aware that it's not recommended to
store large objects in session variables, though. The Session object is
relatively long lived, and the memory resources that it uses will be
reserved for the entire lifetime of the object.
 
Göran Andersson said:
Jeff wrote:

If you want to retain the data from one request to another, you can store
it in a session variable. Be aware that it's not recommended to store
large objects in session variables, though. The Session object is
relatively long lived, and the memory resources that it uses will be
reserved for the entire lifetime of the object.

Okay, I understand the drawbacks, but I need this anyway. I'll have a fixed
and limited number of users at one time.
Suppose that I have either:

Dim Application("MyTable1") As DataTable <- error at this point
or
Dim Session("MyTable1") As DataTable

....and then code to fill the table with data from a DB and then want to use
code similar to that below.
(obviously with the intent of using different rows and columns in different
subs on the same and different web pages)

Label1.Text = Application("MyTable1").Rows(0)(0)

I get no indication that there is an error until I actually run the code.
Then the first line draws the following error.
"Conversion from string "MyTable1" to type 'Integer' is not valid."

What's wrong?

Jeff
 
Dim Application("MyTable1") As DataTable <- error at this point
or
Dim Session("MyTable1") As DataTable
Dim MyTable1 as DataTable = Session("MyTable1")

The rest as Goran explained

Cor
 
Back
Top