views:

49

answers:

1

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?

+1  A: 

Try

HttpContext.Current.Session("x")= x
James Westgate
Thanks James. My one question is is there a way to do a Imports System.Web.HttpContext or something so I don't have to define HttpContext.Current each time? That gets a bit unwieldy.
davemackey
No. HttpContext is static, instance is an object reference.it would normally be System.Web.HttpContext.Current.
James Westgate