Property如何在C#中保持状态? [英] How Property maintain the state in C# ?

查看:86
本文介绍了Property如何在C#中保持状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个类为

I wrote a class as

public class Customer
{
public Customer()
{
//
// TODO: Add constructor logic here
//
}

private static string custName;

public static string CustomerName
{
get { return Customer.custName; }
set { Customer.custName = value; }
}



<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>&nbsp;<asp:Button ID="Button1"
runat="server" Text="Set CustomerName" onclick="Button1_Click" /><br />
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>&nbsp;
<asp:Button ID="Button2" runat="server" Text="Get CustomerName"
onclick="Button2_Click" />
</div>


在button1上,我正在设置CustomerName,在button2上,我正在获取CustomerName

该变量在类中是静态的

我的问题是,当我将客户名称设置为"somexyz"并在我说"get"时将其取回时,它如何保留值?



是否可以使用非静态变量执行此操作,如果是,那么如何?


on button1 i am setting CustomerName and on on button2 i am getting CustomerName

the variable is priavte static in a class

My question how it hold the value when i set a customer name to "somexyz" and get it back when i am saying get?



Is it possible to do it with non-static variable ,if yes then how?

推荐答案

您的问题尚不清楚.

当然可以在不使用静态数据的情况下存储数据(我怀疑在任何情况下静态数据都是错误的).

您是否要在http请求/响应调用之间保留该值.如果是这样,Google会显示"ASP.NET会话状态".该数据仅在会话存在时才存在,不适合长期存储.如果系统处于负载平衡的环境中,则还需要做一些额外的工作.

如果需要长期存储,则需要保留到数据库.您需要为此使用Google ADO.NET,但是您可能希望查找"LINQ To SQL"和"Entity Framework"以获得其他更适合您需要的持久性选项.
[更新]
正如我的评论所承诺的那样:
http://msdn.microsoft.com/en-us/library/ms178581.aspx [ ^ ]
Your question isn''t clear.

Of course it is possible to store data without the use of static (I suspect static is wrong in any case).

Do you want to persist the value between http request/response calls. If so Google for "ASP.NET session state". This data only exists as long as the session exists, it is not suitable for long-term storage. It also takes some extra work if the system is on a load-balanced environment.

If you need long term storage, you will need to persist to database. You need to google ADO.NET for this, but you might well want to look up "LINQ To SQL" and "Entity Framework" for other persistance options that may be better suited to your needs.
[Update]
As promised in my comment:
http://msdn.microsoft.com/en-us/library/ms178581.aspx[^]


这篇关于Property如何在C#中保持状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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