我们可以将会话值分配给静态变量。 。 ? [英] can we assign a session value to a static variable . . ?

查看:54
本文介绍了我们可以将会话值分配给静态变量。 。 ?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨frnds,

我在类中声明了一个静态变量,我需要将会话值分配给该静态变量,如下所示。 。

  public   partial   class  Home_sales:System.Web.UI.Page 
{
static string id = Session [ id]。ToString();
}





但我收到此编译错误

错误 26  对象参考 >  非静态字段,方法或属性'  System.Web.UI.Page.Session.get' 





请告诉我们如何分配会话值到静态变量。 ..请帮帮我



谢谢

mani。 。

解决方案

是的,你可以从非静态函数赋值给静态变量。你不能以这种方式分配会话值,你必须像这样创建函数... :)



  public   partial   class  ShipmentOverview:System.Web.UI .Page 
{
public static string SessionData = string .Empty;

public void SetSession(){


SessionData = Session [ id]。ToString();

}

}



反过来,你不能直接将静态变量分配给静态函数。


hi frnds ,
i have declares a static variable in a class and i need to assign session value to that static variable like this below . .

public partial class Home_sales : System.Web.UI.Page
{
static string id = Session["id"].ToString();
}



but i am getting this compilation error

Error	26	An object reference is required for the non-static field, method, or property 'System.Web.UI.Page.Session.get'	



please tell me guys how to assign a session value to static variable . .. please help me

thanks
mani. .

解决方案

yes you can assign value to static variable from non static function. you can not assign session value in this manner, you have to create function for that like this... :)

public partial class ShipmentOverview : System.Web.UI.Page
    {
        public static string SessionData = string.Empty;

        public void SetSession() {

            
            SessionData = Session["id"].ToString();
        
        }

}


In reverse you can not assign non static variable to static function directly.


这篇关于我们可以将会话值分配给静态变量。 。 ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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