S
syssyx
I have a "CurrentUser" object in session that I want to access from
each page of a vb.net website. I can successfully access everything if
I include the following at the start of each pageload:
Dim objCurrentUser As WebsiteClass.CurrentUser =
Session("CurrentUser")
If objCurrentUser Is Nothing Then objCurrentUser = New
WebsiteClass.CurrentUser
Any page can be linked to directly and not force a login, so without
the second line I get an object instance error. Right now I only save
the CurrentUser object to session on a successful login:
If objCurrentUser.IsLoggedIn Then Session.Add("CurrentUser",
objCurrentUser)
Is there a way to include this logic in the class's new method itself,
such as:
Namespace WebsiteClass
Public Class CurrentUser
...
Public Sub New()
If Not HttpContext.Current.Session("CurrentUser") Is Nothing Then
Me = HttpContext.Current.Session("CurrentUser")
End If
End Sub
End Class
End Namespace
so that each codebehind would simply need:
Dim objCurrentUser As New WebsiteClass.CurrentUser
The trouble I have is I can't seem to assign one instance of a class to
another from within a method of the class, aka "'Me' cannot be the
target of an assignment." Can this be done, or is this unavoidable
based on the structure of OOP, or is there another way to move the
boolean session operation that will need to occur on each page to the
class itself? Thanks in Advance!
Brian
each page of a vb.net website. I can successfully access everything if
I include the following at the start of each pageload:
Dim objCurrentUser As WebsiteClass.CurrentUser =
Session("CurrentUser")
If objCurrentUser Is Nothing Then objCurrentUser = New
WebsiteClass.CurrentUser
Any page can be linked to directly and not force a login, so without
the second line I get an object instance error. Right now I only save
the CurrentUser object to session on a successful login:
If objCurrentUser.IsLoggedIn Then Session.Add("CurrentUser",
objCurrentUser)
Is there a way to include this logic in the class's new method itself,
such as:
Namespace WebsiteClass
Public Class CurrentUser
...
Public Sub New()
If Not HttpContext.Current.Session("CurrentUser") Is Nothing Then
Me = HttpContext.Current.Session("CurrentUser")
End If
End Sub
End Class
End Namespace
so that each codebehind would simply need:
Dim objCurrentUser As New WebsiteClass.CurrentUser
The trouble I have is I can't seem to assign one instance of a class to
another from within a method of the class, aka "'Me' cannot be the
target of an assignment." Can this be done, or is this unavoidable
based on the structure of OOP, or is there another way to move the
boolean session operation that will need to occur on each page to the
class itself? Thanks in Advance!
Brian