开发者

Shared variables in ASP.NET I'm Confused?

开发者 https://www.devze.com 2023-04-02 07:06 出处:网络
I\'m a little confused... Im having trouble with my web application. Im 开发者_运维问答building a system that generates quotes for customers. The customer requests a quote then multiple generated quot

I'm a little confused... Im having trouble with my web application. Im 开发者_运维问答building a system that generates quotes for customers. The customer requests a quote then multiple generated quotes can be sent out to them based on the original request.

In my .vb file I have:

Partial Class NewQuote
Private Shared GeneratedQuoteIDArray As New ArrayList

this GeneratedQuoteIDArray is then accessed and built up from several Protected Sub's.

I'm having difficulty in multi user environment. If someone is working on the same page the GeneratedQuoteIDArray is holding both users values :S.

Please help... I'm sure im just doing something stupid!


Shared means that it's a static value which is attached to the definition of the class itself, not to any particular instance of it. So there's only one within the context of the application. That's why other code paths would change the same value and over-write each other's values.

It sounds like you're looking for an instance value, not a static value. Like this:

Private GeneratedQuoteIDArray As New ArrayList

That way it'll be attached to any new instance of the class and private only to that instance. So separate requests with separate instances will have separate values.

You can read more about Shared members here, as well as many other places. When researching this topic, the keywords you're looking for are "static vs. instance" in terms of variables, functions, etc. You'll likely find a lot of C-style code explaining it, but you can think of it the same way in VB. VB just calls it Shared instead of static.


Keep per-user data somewhere either in Session or in Cookie.

0

精彩评论

暂无评论...
验证码 换一张
取 消