Dave Mackey
Dave Mackey

Reputation: 4432

Using Session State in Public Class in ASP.NET?

I'm trying to move some reusable portions of code into a class. This is working okay except when I attempt to use Session within this class. I get an error:

"Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class."

My code looks something like this:

Public Class webHousing
    Inherits System.Web.UI.Page
  Public Sub GetUserInfo()
    Dim x as String
    x = 10
    Session("x")= x
  End Sub
End Class

I've simplified this code significantly - but the basic problem is present - trying to set a session value from within a class. I found the following MSDN article1 but don't believe Public/Shared can be used on Session?

Upvotes: 0

Views: 1235

Answers (1)

James Westgate
James Westgate

Reputation: 11454

Try

HttpContext.Current.Session("x")= x

Upvotes: 1

Related Questions