小编ron*_*niX的帖子

在c#类中设置全局变量

我试图在一个页面请求期间设置一个带有实时的全局变量.

在经典als中我使用这样:


dim VariableName
VariableName = "test";

sub testsub()
    VariableName += VariableName + "new"
    response.write VariableName
end sub

response.write VariableName '-> test
testsub() '-> testnew
Run Code Online (Sandbox Code Playgroud)

现在在asp.net中我尝试在我的类中设置变量,如下所示:

public static class MyClass
{
    public static string GlobalVar = "test";

    public static string MyMethod()
    {
        GlobalVar += GlobalVar + "new";

        return GlobalVar;
    }
}
Run Code Online (Sandbox Code Playgroud)

但现在,问题是,这个变量就像一个应用程序变量,具有超过所有pagerequest的生命周期.

在一个请求期间,我可以在哪里定义具有生命周期的变量,并且在所有方法和其他类中都可以使用?

c# asp.net global-variables

0
推荐指数
1
解决办法
1373
查看次数

标签 统计

asp.net ×1

c# ×1

global-variables ×1