ASP .NET / VB .NET中的静态变量 [英] Static variables in ASP .NET/VB .NET

查看:59
本文介绍了ASP .NET / VB .NET中的静态变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨聪明人:-)


我注意到很多人声明不使用静态变量和

ASP.NET,以及,据我所知,原因是因为变量是在用户会话中共享的
- 这是非常糟糕的(tm),原因是我理解!


但是,此规则是否仅适用于全局静态变量,或者它是否适用于过程级静态变量。


例如如果我有课:

==================================== ==============

公共类MyPretendClass

继承System.Web.UI.Page

ReadOnly物业SomeProperty()作为整数

获取

静态我作为整数= -1

如果i = -1那么

i = Integer.Parse(Request.Params(" MyQueryStringArgumen t)))

结束如果

返回t

结束获取

结束财产


''REST类,包括使用SomeProperty财产

结束班

= =================================================


我将SomeProperty输出到asp:标签如果我有2个

用户(Alice和Bob),它将如何工作,其中Alice请求页面查询字符串

值为1,然后Bob访问值为2的页面。


显然Alice会在标签中有1个,但是值是多少鲍勃会有吗?


非常感谢,


RB。

Hi clever people :-)

I''ve noticed a lot of people stating not to use static variables with
ASP.NET, and, as I understand it, the reason is because the variable is
shared across user sessions - which is Very Bad (tm) for reasons I
understand!

However, does this rule apply only to global static variables, or does
it apply to procedure-level static variables.

e.g. If I have a class:
==================================================
Public Class MyPretendClass
Inherits System.Web.UI.Page
ReadOnly Property SomeProperty() As Integer
Get
Static i As Integer = -1
If i = -1 Then
i = Integer.Parse(Request.Params("MyQueryStringArgumen t"))
End If
Return t
End Get
End Property

'' REST OF CLASS, INCLUDING USE OF SomeProperty PROPERTY
End Class
==================================================

and I output SomeProperty to an asp:Label how will it work if I have 2
users (Alice and Bob), where Alice requests the page with a query string
value of 1, and Bob then accesses the page with a value of 2.

Obviously Alice will have 1 in the label, but what value will Bob have?

Many thanks,

RB.

推荐答案

您的初始陈述是错误的。如果静态

变量填充了手头的任务,那么它们是全局变量没有任何问题。或者在一个程序

级别。


" RB" < ow ************ @ mailinator.com写在留言中

新闻:2O ****************** *** @ eclipse.net.uk ...
Your initial statement is false. There is nothing wrong with static
variables if they fill the task at hand, be they "global" or at a procedure
level.

"RB" <ow************@mailinator.comwrote in message
news:2O*********************@eclipse.net.uk...

嗨聪明人:-)


我已经注意到很多人声明不要使用静态变量和

ASP.NET,而且据我所知,原因是因为变量是在用户会话间共享的
- 这是非常糟糕(tm)因为我理解了这个原因!


但是,这条规则是否仅适用于全局静态变量,或者是否/>
适用于过程级静态变量。


例如如果我有课:

==================================== ==============

公共类MyPretendClass

继承System.Web.UI.Page

ReadOnly物业SomeProperty()作为整数

获取

静态我作为整数= -1

如果i = -1那么

i = Integer.Parse(Request.Params(" MyQueryStringArgumen t)))

结束如果

返回t

结束获取

结束财产


''REST类,包括使用SomeProperty财产

结束班

= =================================================


我将SomeProperty输出到asp:标签如果我有2个

用户(Alice和Bob),它将如何工作,其中Alice请求页面查询字符串

值为1,然后Bob访问值为2的页面。


显然Alice会在标签中有1个,但是值是多少鲍勃会有吗?<非常感谢
非常感谢,


RB。
Hi clever people :-)

I''ve noticed a lot of people stating not to use static variables with
ASP.NET, and, as I understand it, the reason is because the variable is
shared across user sessions - which is Very Bad (tm) for reasons I
understand!

However, does this rule apply only to global static variables, or does it
apply to procedure-level static variables.

e.g. If I have a class:
==================================================
Public Class MyPretendClass
Inherits System.Web.UI.Page
ReadOnly Property SomeProperty() As Integer
Get
Static i As Integer = -1
If i = -1 Then
i = Integer.Parse(Request.Params("MyQueryStringArgumen t"))
End If
Return t
End Get
End Property

'' REST OF CLASS, INCLUDING USE OF SomeProperty PROPERTY
End Class
==================================================

and I output SomeProperty to an asp:Label how will it work if I have 2
users (Alice and Bob), where Alice requests the page with a query string
value of 1, and Bob then accesses the page with a value of 2.

Obviously Alice will have 1 in the label, but what value will Bob have?

Many thanks,

RB.



公平点 - 我收回原来的陈述,但保留其余的

问题;-)


干杯,

RB。


Aidy写道:
Fair point - I retract my original statement, but leave the rest of the
question open ;-)

Cheers,

RB.

Aidy wrote:

您的初始陈述是错误的。如果静态

变量填充了手头的任务,那么它们是全局变量没有任何问题。或者在一个程序

级别。


" RB" < ow ************ @ mailinator.com写在留言中

新闻:2O ****************** *** @ eclipse.net.uk ...
Your initial statement is false. There is nothing wrong with static
variables if they fill the task at hand, be they "global" or at a procedure
level.

"RB" <ow************@mailinator.comwrote in message
news:2O*********************@eclipse.net.uk...

>嗨聪明人:-)

我注意到了很多人都说不要在ASP.NET中使用静态变量,而且据我所知,原因是因为变量是在用户会话之间共享的 - 这是非常糟糕的(tm)原因我理解了!

但是,此规则是否仅适用于全局静态变量,还是适用于过程级静态变量。

例如如果我有课:
======================================== ========= =
公共类MyPretendClass
继承System.Web.UI.Page
ReadOnly属性SomeProperty()作为整数
获取
静态我作为整数= -1
如果i = -1那么
i = Integer.Parse(Request.Params(" MyQueryStringArgumen t)))
结束如果
返回t <结束获取
结束财产

''静止类,包括使用某些属性
最终类
========== ======================================= =

我将SomeProperty输出到asp:标签如果我有2个
用户(Alice和Bob),其中Alice请求查询字符串值为1的页面,它将如何工作,然后Bob访问该页面值得2.

显然爱丽丝的标签上会有1个,但鲍勃有什么价值?

非常感谢,

RB。
>Hi clever people :-)

I''ve noticed a lot of people stating not to use static variables with
ASP.NET, and, as I understand it, the reason is because the variable is
shared across user sessions - which is Very Bad (tm) for reasons I
understand!

However, does this rule apply only to global static variables, or does it
apply to procedure-level static variables.

e.g. If I have a class:
================================================= =
Public Class MyPretendClass
Inherits System.Web.UI.Page
ReadOnly Property SomeProperty() As Integer
Get
Static i As Integer = -1
If i = -1 Then
i = Integer.Parse(Request.Params("MyQueryStringArgumen t"))
End If
Return t
End Get
End Property

'' REST OF CLASS, INCLUDING USE OF SomeProperty PROPERTY
End Class
================================================= =

and I output SomeProperty to an asp:Label how will it work if I have 2
users (Alice and Bob), where Alice requests the page with a query string
value of 1, and Bob then accesses the page with a value of 2.

Obviously Alice will have 1 in the label, but what value will Bob have?

Many thanks,

RB.



C你不只是测试得到答案吗?或者你可以不打扰你做自己的功课,尽管它只涉及输入一个功能和按b5按b
?鲍勃也将拥有1(假设他在爱丽丝之后以合理的足够时间查看该网站),当然)


RB < ow ************ @ mailinator.com写在留言中

新闻:hJ ****************** *** @ eclipse.net.uk ...
Can''t you just test to get the answer? Or can you not be bothered doing
your own homework despite it only involving typing in one function and
pressing F5? Bob will also have 1 (assuming he viewed the site in a
reasonable enough time after Alice, of course)

"RB" <ow************@mailinator.comwrote in message
news:hJ*********************@eclipse.net.uk...

公平点 - 我收回原来的陈述,但留下其余的

问题公开;-)

干杯,

RB。


Aidy写道:
Fair point - I retract my original statement, but leave the rest of the
question open ;-)

Cheers,

RB.

Aidy wrote:

>您的初始陈述为假。如果静态
变量填满了手头的任务,那么它们是全局的变量是没有错的。或者在程序级别。

RB < ow ************ @ mailinator.com写的消息
新闻:2O ********************* @ eclipse.net.uk ...
>Your initial statement is false. There is nothing wrong with static
variables if they fill the task at hand, be they "global" or at a
procedure level.

"RB" <ow************@mailinator.comwrote in message
news:2O*********************@eclipse.net.uk...

>>嗨聪明的人:-)

我注意到了很多人们声明不要在ASP.NET中使用静态变量,而且据我所知,原因是因为变量是在用户会话中共享的 - 这是非常糟糕的(tm),原因是我<理解!

但是,此规则是否仅适用于全局静态变量,或者它是否适用于过程级静态变量。

例如如果我有课:
======================================== ======== ==
公共类MyPretendClass
继承System.Web.UI.Page
ReadOnly属性SomeProperty()作为整数
获取
静态我作为整数= -1
如果i = -1那么
i = Integer.Parse(Request.Params(" MyQueryStringArgumen t)))
结束如果
返回t <结束获取
结束财产

''静止类,包括使用某些属性
最终类
========== ====================================== ==

我将SomeProperty输出到asp:标签如果我有2个
用户(Alice和Bob),其中Alice请求查询字符串值为1的页面,它将如何工作,然后Bob访问该页面值得2.

显然爱丽丝的标签上会有1个,但鲍勃有什么价值?

非常感谢,

RB。
>>Hi clever people :-)

I''ve noticed a lot of people stating not to use static variables with
ASP.NET, and, as I understand it, the reason is because the variable is
shared across user sessions - which is Very Bad (tm) for reasons I
understand!

However, does this rule apply only to global static variables, or does
it apply to procedure-level static variables.

e.g. If I have a class:
================================================ ==
Public Class MyPretendClass
Inherits System.Web.UI.Page
ReadOnly Property SomeProperty() As Integer
Get
Static i As Integer = -1
If i = -1 Then
i = Integer.Parse(Request.Params("MyQueryStringArgumen t"))
End If
Return t
End Get
End Property

'' REST OF CLASS, INCLUDING USE OF SomeProperty PROPERTY
End Class
================================================ ==

and I output SomeProperty to an asp:Label how will it work if I have 2
users (Alice and Bob), where Alice requests the page with a query string
value of 1, and Bob then accesses the page with a value of 2.

Obviously Alice will have 1 in the label, but what value will Bob have?

Many thanks,

RB.


这篇关于ASP .NET / VB .NET中的静态变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆