Property如何在C#中保持状态?

编程入门 行业动态 更新时间:2024-10-27 00:33:15
本文介绍了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"以获得其他更适合您需要的持久性选项. [更新] 正如我的评论所承诺的那样: msdn.microsoft/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: msdn.microsoft/en-us/library/ms178581.aspx[^]

更多推荐

Property如何在C#中保持状态?

本文发布于:2023-11-06 17:47:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1564372.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:状态   如何在   Property

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!