'System.Web.HttpContext.Session' is a 'property' but is used like a 'method

Hello, I'm trying to set my sessions into a class so I can set and access it easily.

I get a 'System.Web.HttpContext.Session' is a 'property' but is used like a 'method' error, can you see what's wrong with my code ?

 

Thanks 

 

public class SessionManager
    {
       
        public static bool isLogged
        {
            get
            {
                return  HttpContext.Current.Session("isLogged");
            }
        }
        public static string accountId
        {
            get
            {
                return HttpContext.Current.Session("accountId");
            }
        }

    } 

0
Sleb
12/15/2007 8:58:46 PM
asp.net.state-management 8807 articles. 0 followers. Follow

2 Replies
2035 Views

Similar Articles

[PageSpeed] 18

Session variables are accessible by an indexer. Also, session returns session variables as an object, so you need to case it. like this: 

public class SessionManager
{
	public static bool isLogged
	{
		get
		{
			return (bool)HttpContext.Current.Session["isLogged"];
		}
	}
	public static string accountId
	{
		get
		{
			return (string)HttpContext.Current.Session["accountId"];
		}
	}
} 

 

You should also do a null check on your session variables before casting them to their type to avoid exceptions.


Cheers,
       Kevin Jones


0
vcsjones
12/15/2007 9:13:19 PM

Thanks , it works now .

 

0
Sleb
12/15/2007 9:53:30 PM
Reply:

Web resources about - 'System.Web.HttpContext.Session' is a 'property' but is used like a 'method - asp.net.state-management

Resources last updated: 3/13/2016 3:02:11 AM