由于我自学成才,我的VB编码也不错,但我对OOP的使用很差。我确信这可以做到,但我还没有发现。
我正在构建一个webforms应用程序,需要从AD中获取有关用户的数据。我有一个Person Class,我可以使用如下
Public Class _Default
Inherits System.Web.UI.Page
Dim LoggedOnPerson As Person 'Added here so available throughout class
Private strLoggedOnUser As String
Private strADDomain As String
Private strADUserID As String
Public Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
strLoggedOnUser = Request.ServerVariables("LOGON_USER").ToUpper
strADDomain = strLoggedOnUser.Split("\")(0)
strADUserID = strLoggedOnUser.Split("\")(1)
If Not IsPostBack Then
'Dim LoggedOnPerson As Person *** changed to
LoggedOnPerson = New Person
'Get details from AD for logged on user
LoggedOnPerson.GetDetails(strADDomain, strADUserID)
'Store in Session
Session("LoggedOnUser") = LoggedOnUser
'This will now give me access to details such as
'LoggedOnPerson.EmailAddress
'LoggedOnPerson.GivenName
'LoggedOnPerson.TelephoneNo etc.
Else
'Postback so pull in details from Session
LoggedOnUser = Session("LoggedOnUser")
End If
End Sub
End Class
我的问题是我无法在其他事件中访问LoggedOnPerson。 e.g。
Public Sub SaveDetails()
Dim email As String = LoggedOnPerson.Email
'This now produces correct result. No error that LoggedOnPerson is not declared
End Sub
我当然得到LoggedOnPerson没有声明错误。我怎么能解决这个问题。
答案 0 :(得分:1)
您已在Page_Load事件中创建了“Person”对象。把它带到外面并在班级宣布。还要添加该对象以在Page_Load事件上查看状态/会话状态,并将其类型转换为其他事件中的“Person”类。