如何在app_code类文件中获取应用程序变量? [英] how to get the application variable in app_code class file?

查看:109
本文介绍了如何在app_code类文件中获取应用程序变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个全局变量例如:

i have create a global varible For example:

Application["userName"] = "Jhons Thomas";



现在我想在类文件中获取此应用程序变量的值,例如:


Now i want to get the value of this application variable in class file for example:

class A{
   string user;
   user = Application["userName"];
}



但是我知道这不是获得此变量的正确方法。任何人都可以让我知道如何获得这个变量吗?


But i know this not right way to get this variable.Can anyone let me know how to get this varibale?

推荐答案

请看这里:

如何:在应用程序状态中保存值 [ ^ ]

如何:从应用程序状态读取值 [ ^ ]



我认为使用应用程序为class设置默认值的变量是错误的。不这样做是有原因的。

如果要设置默认值,可以使用类(实例)构造函数 [ ^ ]。

Please, have a look here:
How to: Save Values in Application State[^]
How to: Read Values from Application State[^]

I think that using application variable to set default value for class is wrong. It's set of reason to not do that.
If you want to set default value, you can use class (instance) constructor[^].
class A
{
   private string sUName = string.Empty;

    public A(string _UName)
    {
        sUName = _UName;
    }

    public string UserName
    {
        get{return sUName;}
        set{sUName = value;}
    }
}



用法:


Usage:

A a = new A("John Doe");
Console.WriteLine(A.UserName());





详情请见:构造函数(C#编程指南) [ ^ ]


HttpContext.Current.Application [username]
HttpContext.Current.Application["username"]


这篇关于如何在app_code类文件中获取应用程序变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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