关于持久性和全局变量的问题 [英] Question about persistence and a global variable

查看:55
本文介绍了关于持久性和全局变量的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


任何人都可以告诉我,如果我在我的页面代码中声明了一个全局变量,

如果页面执行的话,它会持续存在吗?回发,或者我是否需要将对象

添加到会话对象中以便保留它。


会话是持久化的最佳机制对象


谢谢大家


Simon

解决方案

你好,Simon,

请定义全局变量。发布一些简短的例子,也许有人

将能够回答你的问题。


问候

马丁

Simon Harvey < SI ********** @ the-web-works.co.uk>在消息中写道

新闻:OO ************* @ TK2MSFTNGP09.phx.gbl ...

大家好,
任何人都可以告诉我,如果我在我的页面代码中声明了一个全局变量,
如果页面回发后它是否持久存在,或者我是否需要将
对象添加到会话对象是为了坚持它。

会话是持久化这个对象的最佳机制

谢谢大家

Simon






全局变量我指的是一个已在类中声明的变量

但不包括任何方法。


例如


公共类BoB {

私人字符串名=鲍勃; //变量是全局的


公共Bob(){


}

}


西蒙,

" Simon Harvey" < SI ********** @ the-web-works.co.uk>在消息中写道

新闻:#K ************** @ TK2MSFTNGP09.phx.gbl ...




公共类BoB {
私人字符串名称= Bob; //变量是全局的


它被称为类的字段,不,它不会在帖子之间保持其值

,因为每个请求都是页面(已发布或未发布)实例化该类的新

实例。如果你想在单个用户的回发之间保留

页面中的内容 - 使用ViewState并将

包装在属性中:


public string Foo

{

get

{

if(ViewState [" Foo"]!= null)

将ViewState [" Foo"]返回为字符串;

返回"" ;; //或某些默认值,或null < br $>
}

set {ViewState [" Foo"] = value;}

}


如果你想为每个用户保留一个值,请使用一个Session项或Cookie。


如果你想在应用程序的生命周期中保留一个值 - 使用

申请项目。或者(但需要更具体的设计),

你可以将数据保存在HttpContext的Cache中。


如果你想保留一个值AppDomain的生命周期(直到你的
程序集从内存中卸载)在某个类中创建一个静态字段。

在这种情况下你可以设置适当的辅助功能修改器 - 私人,

公众等。


我可能错过了一些东西,但这些是最常用的方式。


希望这有帮助

Martin

公共Bob(){

}
}


Hi everyone,

Can anyone tell me if I declare a global variable in my pages code behind,
is it persisted if the page does a post back, or do I need to add the object
to the session object in order to persist it.

Is the session the best mechnism for persisiting this object

Thanks everyone

Simon

解决方案

Hi, Simon,

Please, define "global variable". Post some short example and maybe someone
will be able to answer your question.

Greetings
Martin
"Simon Harvey" <si**********@the-web-works.co.uk> wrote in message
news:OO*************@TK2MSFTNGP09.phx.gbl...

Hi everyone,

Can anyone tell me if I declare a global variable in my pages code behind,
is it persisted if the page does a post back, or do I need to add the object to the session object in order to persist it.

Is the session the best mechnism for persisiting this object

Thanks everyone

Simon



Hi,

By Global Variable I mean a variable that has been declared inside a class
but outside any methods.

eg

public class BoB{
private string name = Bob; // Variable is global

public Bob(){

}
}


Hi, Simon,

"Simon Harvey" <si**********@the-web-works.co.uk> wrote in message
news:#K**************@TK2MSFTNGP09.phx.gbl...

Hi,

By Global Variable I mean a variable that has been declared inside a class
but outside any methods.

eg

public class BoB{
private string name = Bob; // Variable is global

It is called field of the class and, no, it will not persist its value
between the posts because on each request for the page (posted or not) a new
instance of the class is instantiated. If you want to keep something in the
page between postbacks of a single user only - use the ViewState and wrap
the value in a property:

public string Foo
{
get
{
if(ViewState["Foo"] != null)
return ViewState["Foo"] as string;
return "";// or some default value, or null
}
set{ViewState["Foo"] = value;}
}

If you want to persist a value per user use a Session item or Cookie.

If you want to persist a value for the lifetime of the application - use an
Application item. Alternatively (but requires a bit more specific desing),
you can persist data in the Cache of the HttpContext.

If you want to persist a value for the lifetime of the AppDomain (until your
assembly gets unloaded from the memory) create a static field in some class.
You can set the appropriate accessibility modifier in this case - private,
public etc.

I might have missed something but these are the most used ways.

Hope this helps
Martin
public Bob(){

}
}



这篇关于关于持久性和全局变量的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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