Use of shared class members in ASP.NET

M

Mr Newbie

I'm still thinking around this subject, and I find myself unsure about a
couple of things.

When a new users launches an ASP.NET application, this must be on a new
thread. When it encounters a shared member in a class, unless you are
setting static variables presumably when you instantiate an local object in
that shared member function the system maintains a local copy of that object
per thread ( is this right ? ).

To perhaps make my point more clearly. In a shared member function, If I
have a shared member which declares a variable a of type Integer. the actual
storage of this integer I assume would be on the users one thread and cannot
be confused with anyone slses (UNLESS) the variable is static as pointed out
by Jay.

The reason this is making me paranoid is that I want to make extensive used
of shared functions in my data layer to reduce code bloat and constant
instantiation.

Does anyone see any issues with this. Cor's example in the earlier post
related to a dataset does not seem right to me as he says that the dataset
would be overwritten, I cant see how this can be right. I decided to re-do
this posting thread to start afresh, I hope no one minds.

Sorry for being a little paranoid here, but i dont want to embark upon loads
of work and then find out Im screwed!
 
D

David Browne

Mr Newbie said:
I'm still thinking around this subject, and I find myself unsure about a
couple of things.

When a new users launches an ASP.NET application, this must be on a new
thread. When it encounters a shared member in a class, unless you are
setting static variables presumably when you instantiate an local object
in that shared member function the system maintains a local copy of that
object per thread ( is this right ? ).

To perhaps make my point more clearly. In a shared member function, If I
have a shared member which declares a variable a of type Integer. the
actual storage of this integer I assume would be on the users one thread
and cannot be confused with anyone slses (UNLESS) the variable is static
as pointed out by Jay.

Local variables are allocated on the stack, which is memory that belongs
exclusively to the current thread.


David
 
K

Kevin Spencer

I'm a little confused by your use of the word "shared," and in the same
message, "static." In VB.Net, "shared" means "static." So, are you referring
to "shared" as in "static," or for some other meaning? If not "static," what
definition of "shared" are you meaning?

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but you can't make it stink.
 
C

Cor Ligthert [MVP]

Mr, Newbie,
Does anyone see any issues with this. Cor's example in the earlier post
related to a dataset does not seem right to me as he says that the dataset
would be overwritten, I cant see how this can be right.

If you had asked that original thread than I would have explained in another
way for you. As far as I know was you not the OP, if you was the OP, than
please don't confuse us.

Especially do I not understand why you send messages to the framework.aspnet
group, where in you suggest that I give wrong answers in the language.vb
newsgroup, without telling in one word what the question was.

If you are sure that my answer is wrong, than you can start a discussion or
ignore it, however telling it in other newsgroups is for me undecent.

Cor
 
M

Mr Newbie

Sorry, yes I meant the same thing, its an occupational hazard of switching
languages.
 
M

Mr Newbie

Im very sorry. I did not mean to upset you in any way.

I simply became confused with the answers, I should have replied on the
original thread. My appologies.


-
Best Regards

The Inimitable Mr Newbie º¿º
 
K

Kevin Spencer

Hi Mr. N,

Static data is singleton data. That is, there is one copy for the entire
application. There is not one copy per thread.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but you can't make it stink.
 
M

Mr Newbie

Yes, I realise that. My concerns were over non static variables, I was
concerned that there may be scope for overwriting in a multi user
environment, but this was unfounded.
 
C

Cor Ligthert [MVP]

Mr. Newbie,

I simply became confused with the answers, I should have replied on the
original thread. My appologies.

Let me accept it and therefore repeat, a webapplication (is a DLL not an
Exe), is a middle tier application.

It's sharing its shared parts (even its static parts) for all users.

Therefore if you create this construction (which can be done in winforms).
\\\
Public Shared ds as dataset
Public shared sub fillmydataset(byval clientnumber as string)
dim sqlstring as string = "Select from table where client = '" &
clientnumber & "'"
' in a lazy way of coding without parameters
'rest of the code
da.fill(ds)
end sub
///

Than every time that a client does a "fillmydataset", the shared member ds
of the application will be overwritten. You have than therefore in my
opinion not any control about it when this will happen and cannot use that
dataset in your updates even not if you read it very quick in advance.

I hope that this gives an idea

Cor
 
K

Kevin Spencer

Ah, I misunderstood.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but you can't make it stink.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top