473,387 Members | 1,574 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Confusion about shared vs. public vs. ?


....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

--
Posted via a free Usenet account from http://www.teranews.com

Apr 16 '07 #1
3 2331
Jeff wrote:
...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
_____
http://www.guffa.com
Apr 17 '07 #2

"Göran Andersson" <gu***@guffa.comwrote in message
news:uK**************@TK2MSFTNGP03.phx.gbl...
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.

--
Göran Andersson
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
--
Posted via a free Usenet account from http://www.teranews.com

Apr 17 '07 #3
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
Apr 17 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: John Brock | last post by:
I have a base class with several derived classes (I'm writing in VB.NET). I want each derived class to have a unique class ID (a String), and I want the derived classes to inherit from the base...
3
by: Ken Brannigan | last post by:
Hello, I am a little confused regarding the creation of a C# DLL that will be called from ASP.NET. From what I have read ASP.NET is Multithreaded. My undersatnding of this is that more than one...
10
by: darrel | last post by:
I'm still trying to sort out in my head the differences between public and shared when referring to declaring properties or variables. This is my understanding: shared - akin to a 'global'...
11
by: tshad | last post by:
I am setting up some of my functions in a class called MyFunctions. I am not clear as to the best time to set a function as Shared and when not to. For example, I have the following bit...
15
by: Rob Nicholson | last post by:
A consequence of the ASP.NET architecture on IIS has just hit home with a big thud. It's to do with shared variables. Consider a module like this: Public Module Functions Public GlobalName As...
33
by: Joe Fallon | last post by:
1. I have a Base class that has a certain amount of functionality. 2. Then I have a CodeSmith generated class that inherits from the Base class and adds functionality. 3. Since I want to be able...
5
by: Simon | last post by:
Hi all, We have an ASP.NET 1.1 application running on IIS6 on Server 2003. Most of the base objects we are using in this application are taken from a windows application also written by us. We...
8
by: tharringtonan | last post by:
I am compiling the following code, main.cpp, as follows: gcc main.cpp I get the following compile error: main.cpp: In function `int main()': main.cpp:28: no matching function for call to...
6
by: Grok | last post by:
In writing a class library, one of the classes should only ever be instantiated once, and its object should be accessible to every other class in the library. How can I do that? To provide some...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.