使用类一旦它的属性已在c#中设置(Using a class once it's properties have been set in c#)

编程入门 行业动态 更新时间:2024-10-20 20:49:16
使用类一旦它的属性已在c#中设置(Using a class once it's properties have been set in c#)

我有一个小班,只有几个属性。

这是一个例子:

public class clsRepLogs public string those; public string these; public void setThoseandThese { //call a stored procedure //get results this.those = something; this.these = somethingElse; }}

来自我的first.aspx.cs

我调用set函数:

clsRepLogs cLog - new clsRepLogs() cLog.setThoseandThese()

所以现在已经为属性分配了值。

我现在想在另一个aspx.cs文件中使用它们来填充表单......但是无法弄清楚如何找到它们......

我试过了

clsRepLogs cLog; lblThese.text = cLog.these;

但它给了我一个错误:“使用未分配的局部变量'cLog'

基本上,我怎么告诉它使用我之前已分配给该类实例的值?

我希望我能解释这一点,但我可能会对我正在做的事情有所了解。 任何帮助赞赏。

i have a small class with just a couple properties in it.

here is an example:

public class clsRepLogs public string those; public string these; public void setThoseandThese { //call a stored procedure //get results this.those = something; this.these = somethingElse; }}

from my first.aspx.cs

i call the set function:

clsRepLogs cLog - new clsRepLogs() cLog.setThoseandThese()

so now the properties have been assigned values.

i now want to use them in another aspx.cs file to populate a form... but can't figure out how to get to them...

i tried

clsRepLogs cLog; lblThese.text = cLog.these;

but it's giving me an error: "Use of unassigned local variable 'cLog'

basically, how do i tell it to use the values i've already assigned to that class instance from before?

i hope i'm explaining this right, but i might be way of on what i'm doing. any help appreciated.

最满意答案

听起来您想要从多个ASPX页面访问该类的同一实例。 有多种方法可以做到这一点; 每用户解决方案是使用会话状态。

// page 1 var c = new clsRepLogs(); c.setThoseAndThese(); Session["mykey"] = c; // page 2 var c = (clsRepLogs)Session["mykey"];

“mykey”可以是任何字符串。 您可能还想在访问Session之前检查Session是否包含该值,例如if( Session["mykey"] != null ){ ... }

记住:

会话不耐用; 重新启动Web工作进程将重置所有进程内会话。 在Session中存储许多大对象通常不是一个好主意。 如果数据是保密的,请注意会话固定攻击 。 会话可以跨服务器进行负载平衡,因此该解决方案可以扩展。

供参考(替代方法):

如果您不关心每个用户的隔离并希望控制数据过期,则可以使用Cache对象。 您可以使用Application对象; 类似于Cache但无法控制过期。 您可以将对象存储在数据库中并将其加载到每个页面上。 当您不想要繁重的会话和/或希望更永久地保存数据(例如在多个会话中持续存在的购物车)时,这非常有用。

It sounds like you want to access the same instance of the class from multiple ASPX pages. There are multiple ways to do this; a per-user solution is to use session state.

// page 1 var c = new clsRepLogs(); c.setThoseAndThese(); Session["mykey"] = c; // page 2 var c = (clsRepLogs)Session["mykey"];

"mykey" can be any string. You may also want to check if Session contains the value before accessing it, e.g. if( Session["mykey"] != null ){ ... }

Keep in mind:

Session isn't durable; restarting the web worker process will reset all in-process sessions. It's usually not a good idea to store many large objects in Session. If the data is confidential, be aware of Session fixation attacks. Session can be load balanced across servers, so this solution will scale.

For reference (alternative approaches):

You could use the Cache object when you don't care about per-user isolation and want control over data expiration. You could use the Application object; similar to Cache but no control over expiration. You could store the object in the database and load it on each page. This is useful when you to don't want a heavy session, and/or wish to save the data more permanently (such as a shopping cart which persists across multiple sessions).

更多推荐

本文发布于:2023-08-04 22:16:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1423341.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:已在   属性   类一旦   set   properties

发布评论

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

>www.elefans.com

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