本文介绍了sub new()中的OOP对象实例赋值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有一个CurrentUser我希望从vb.net网站的每个页面访问的会话中的对象。我可以成功访问所有内容如果 我在每个页面加载的开头包括以下内容: Dim objCurrentUser As WebsiteClass.CurrentUser = 会话(" CurrentUser") 如果objCurrentUser是Nothing那么objCurrentUser =新 WebsiteClass.CurrentUser 任何页面可以直接链接而不强制登录,所以没有 第二行我得到一个对象实例错误。现在我只在成功登录时将CurrentUser对象保存到会话中: 如果objCurrentUser.IsLoggedIn那么Session.Add(" CurrentUser", objCurrentUser) 有没有办法在类的新方法中包含这个逻辑, 如: 命名空间WebsiteClass 公共类CurrentUser ... Public Sub New() 如果不是HttpContext.Current.Session(" CurrentUser")则没有呢 Me = HttpContext.Current.Session(" CurrentUser") 结束如果 结束次级 结束班级 结束命名空间 以便每个代码隐藏只需要: Dim objCurrentUser As New WebsiteClass.CurrentUser 我遇到的麻烦是我似乎无法分配一个实例在班级的一个方法中,另一个班级来自另一个班级,又名'我''不能是 作业的目标。可以这样做,或者这是不可避免的基于OOP结构的,还是有另一种方法可以移动每个需要发生的 布尔会话操作页面到 类本身?提前致谢! Brian I have a "CurrentUser" object in session that I want to access fromeach page of a vb.net website. I can successfully access everything ifI include the following at the start of each pageload: Dim objCurrentUser As WebsiteClass.CurrentUser =Session("CurrentUser")If objCurrentUser Is Nothing Then objCurrentUser = NewWebsiteClass.CurrentUser Any page can be linked to directly and not force a login, so withoutthe second line I get an object instance error. Right now I only savethe 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 WebsiteClassPublic Class CurrentUser...Public Sub New()If Not HttpContext.Current.Session("CurrentUser") Is Nothing ThenMe = HttpContext.Current.Session("CurrentUser")End IfEnd SubEnd ClassEnd 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 toanother from within a method of the class, aka "''Me'' cannot be thetarget of an assignment." Can this be done, or is this unavoidablebased on the structure of OOP, or is there another way to move theboolean session operation that will need to occur on each page to theclass itself? Thanks in Advance! Brian 推荐答案 这篇关于sub new()中的OOP对象实例赋值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-16 09:09