sub new()中的OOP对象实例赋值 [英] OOP object instance assignment in sub new()

查看:64
本文介绍了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 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

推荐答案

Syssex,


也许我理解你错了,但为什么不把新声明传递给

设置。

请注意ASPNET是无状态的,这个意味着在将页面发送回用户之后,每个对象都会自动处理。


我认为你想要的东西(未经测试)


如果Session.item(用户)什么都没有那么

重定向到主页以进行验证

else

ThisUser =新用户(Session.Item(用户))

结束如果


公共类用户

Public Sub新的(byval TheUser as String)

做你想做的事情

结束sub

结束课

..

我希望这会有所帮助,

Cor


< sy **** @ gmail.com> schreef in bericht

news:11 ********************* @ j55g2000cwa.googlegro ups.com ...
Syssex,

Maybe I understand you wrong but why don''t you pass the New statement to the
setting.
Be aware that ASPNET is stateless, this means that every object is
automaticly disposed after that the page is sent back to the user.

I think that you want something as (not tested)

If Session.item(user) Is Nothing then
redirect to home page for validation purpose
else
ThisUser = New User(Session.Item(user))
End if

Public class User
Public Sub New(byval TheUser as String)
Do the things you want to do
End sub
End Class
..
I hope this helps,
Cor

<sy****@gmail.com> schreef in bericht
news:11*********************@j55g2000cwa.googlegro ups.com...
我有一个CurrentUser我希望从vb.net网站的每个页面访问的会话中的对象。我可以成功访问所有内容如果我在每个页面加载的开头包含以下内容:

Dim objCurrentUser As WebsiteClass.CurrentUser =
Session(CurrentUser)
如果objCurrentUser是Nothing那么objCurrentUser =新
WebsiteClass.CurrentUser

任何页面都可以直接链接而不强制登录,所以没有
第二行我得到一个对象实例错误。现在我只在成功登录时将CurrentUser对象保存到会话:

如果objCurrentUser.IsLoggedIn那么Session.Add(CurrentUser,
objCurrentUser)
有没有办法将这种逻辑包含在类的新方法本身中,例如:

命名空间WebsiteClass
公共类CurrentUser ...
Public Sub New()
如果不是HttpContext.Current.Session(" CurrentUser")则什么都没有
我= HttpContext.Current.Session(" CurrentUser")
结束如果
End Sub
End Class
结束命名空间

这样每个代码隐藏都只需要:

Dim objCurrentUser作为新的WebsiteClass.CurrentUser

我遇到的麻烦是我似乎无法从类的方法中将一个类的实例分配给另一个类,也就是''我不能成为作业的目标。可以这样做,或者这是不可避免的基于OOP的结构,还是有另一种方法将需要在每个页面上发生的
布尔会话操作移动到
类本身?提前致谢!

Brian
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



您不能为Me关键字指定任何内容。构造函数(Sub New)

不能返回null引用,它总是返回类的实例。


使用工厂方法获取或者创建实例:


Public Shared Sub GetCurrent()As CurrentUser

Dim objCurrentUser As WebsiteClass.CurrentUser


objCurrentUser = Session(" CurrentUser")

如果objCurrentUser为Nothing那么

objCurrentUser =新的WebsiteClass.CurrentUser

结束如果

返回objCurrentUser

结束子


如果你想使用构造函数,你就不能返回实际的对象
会话变量中的
,您必须将该对象的信息从

复制到新创建的实例中。那当然意味着

,你会有两个独立但相同的实例,一个通过会话变量引用

,一个在页面中创建。
sy **** @ gmail.com 写道:
You can''t assign anything to the Me keyword. A constructor (Sub New)
can''t return a null reference, it always returns an instance of the class.

Use a factory method to get or create the instance:

Public Shared Sub GetCurrent() As CurrentUser
Dim objCurrentUser As WebsiteClass.CurrentUser

objCurrentUser = Session("CurrentUser")
If objCurrentUser is Nothing Then
objCurrentUser = New WebsiteClass.CurrentUser
End If
Return objCurrentUser
End Sub

If you want to use the constructor, you can''t return the actual object
in the session variable, you would have to copy the information from
that object into the newly created instance. That would of course mean
that you would have two separate but identical instances, one referenced
by the session variable and one created in the page.
sy****@gmail.com wrote:
我有一个CurrentUser我希望从vb.net网站的每个页面访问的会话中的对象。我可以成功访问所有内容如果我在每个页面加载的开头包含以下内容:

Dim objCurrentUser As WebsiteClass.CurrentUser =
Session(CurrentUser)
如果objCurrentUser是Nothing那么objCurrentUser =新
WebsiteClass.CurrentUser

任何页面都可以直接链接而不强制登录,所以没有
第二行我得到一个对象实例错误。现在我只在成功登录时将CurrentUser对象保存到会话:

如果objCurrentUser.IsLoggedIn那么Session.Add(CurrentUser,
objCurrentUser)
有没有办法将这种逻辑包含在类的新方法本身中,例如:

命名空间WebsiteClass
公共类CurrentUser ...
Public Sub New()
如果不是HttpContext.Current.Session(" CurrentUser")则什么都没有
我= HttpContext.Current.Session(" CurrentUser")
结束如果
End Sub
End Class
结束命名空间

这样每个代码隐藏都只需要:

Dim objCurrentUser作为新的WebsiteClass.CurrentUser

我遇到的麻烦是我似乎无法从类的方法中将一个类的实例分配给另一个类,也就是''我不能成为作业的目标。可以这样做,或者这是不可避免的基于OOP的结构,还是有另一种方法将需要在每个页面上发生的
布尔会话操作移动到
类本身?提前致谢!

Brian
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



Goran,


使用共享课程ASPNET应用程序可能非常危险。

共享类在

应用程序中永远被所有活动用户一起使用。


因此,您的objCurrenUser仅适用于启动

应用程序的第一个用户Nothing。 (只要有用户(会话),就会存在

之后)


我知道这对Winform开发人员来说是第一次混淆见

那个。


Cor
Goran,

Using shared classes in an ASPNET application can be very dangerous.
A shared class is forever used by all active users together in the
application.

Therefore your objCurrenUser, will only be for the first user who starts the
application "Nothing". (As long as there are users (sessions), it will after
that exist)

I know that this is confusing for Winform developers the first time they see
that.

Cor


这篇关于sub new()中的OOP对象实例赋值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆