473,396 Members | 1,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

OOP object instance assignment in sub new()

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

Jun 12 '06 #1
11 4292
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...
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

Jun 13 '06 #2
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:
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

Jun 13 '06 #3
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
Jun 13 '06 #4
You are right about using shared classes in ASP.NET, but I'm not talking
about shared classes at all. The code that I'm showing is a factory
method, just as I mentioned in the post.

If you aren't familiar with the concept, a factory method is used to get
an instance of a class instead of directly using the constructor. A
factory method is usually a shared method, so that you don't have to
create a dummy instance of the class just to be able to call the method.
Cor Ligthert [MVP] wrote:
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

Jun 13 '06 #5
First, thanks Cor and Göran for the help!

The function looks to be just what I wanted as far as putting the test
in one location and not in each script, although it sounds like what
I'm looking for is not the ideal. :) Am I correct that it would only
duplicate the attributes and utilize one copy of the methods? The
CurrentUser object also "has-a" object that accesses CurrentUser
properties, similar to the code in
http://groups.google.com/group/micro...8fa285c54b54ca

Namespace WebsiteClass
Public Class CurrentUser
Public AnotherObject As AnotherClass.AnotherObject
...
Public Sub New()
AnotherObject = New AnotherClass.AnotherObject(me)
End Sub
End Class
End Namespace
Namespace AnotherClass
Public Class AnotherObject
Private _CurrentUser As WebsiteClass.CurrentUser
...
Public Sub New(ByRef CU As
WebsiteClass.CurrentUser)
_CurrentUser = CU
End Sub
Public Sub AnotherFunction()
somehowuse(_CurrentUser.SomeProperty)
End Sub
End Class
End Namespace

Will this passing of the CurrentUser to AnotherClass duplicate the
object methods? I thought ByRef wouldn't and ByVal would, but it looks
like they really mean IN and IN/OUT, so it sounds like I should use
ByVal whenever possible, rather than the other way around. And would
combining this with the function Göran mentioned result in 4
instances? I sound nit-picky but the CurrentUser object is growing
quickly to incorporate a lot of business logic and I'm still trying to
get my arms around all this!

Brian

Jun 13 '06 #6
sy****@gmail.com wrote:
First, thanks Cor and Göran for the help!

The function looks to be just what I wanted as far as putting the test
in one location and not in each script, although it sounds like what
I'm looking for is not the ideal. :) Am I correct that it would only
duplicate the attributes and utilize one copy of the methods?
There is always only one copy of the methods. An instance of an object
doesn't contain the methods, it only has the ability to use the methods.
The compiler keeps track of what objects can use which methods.

An object instance only contains it's (non-shared) member variables.
The
CurrentUser object also "has-a" object that accesses CurrentUser
properties, similar to the code in
http://groups.google.com/group/micro...8fa285c54b54ca

Namespace WebsiteClass
Public Class CurrentUser
Public AnotherObject As AnotherClass.AnotherObject
...
Public Sub New()
AnotherObject = New AnotherClass.AnotherObject(me)
End Sub
End Class
End Namespace
Namespace AnotherClass
Public Class AnotherObject
Private _CurrentUser As WebsiteClass.CurrentUser
...
Public Sub New(ByRef CU As
WebsiteClass.CurrentUser)
_CurrentUser = CU
End Sub
Public Sub AnotherFunction()
somehowuse(_CurrentUser.SomeProperty)
End Sub
End Class
End Namespace

Will this passing of the CurrentUser to AnotherClass duplicate the
object methods?
No, nothing is duplicated. The only thing that is sent to the
constructor of the AnotherClass is a reference to the CurrentUser object.

As you have declared the parameter as ByRef it actually only accepts a
reference variable. As Me is not a variable, the reference is first
copied to a temporary variable, and the reference to the temporary
variable is sent to the constructor. The compiler creates code that does
the same thing as:

Dim temp as CurrentUser = Me
AnotherObject = New AnotherClass.AnotherObject(temp)

As the reference is sent using ByRef, you have the ability to change the
variable used in the method call from within the method. In this case
that would mean that you would change the temporary variable that was
created for the call, but as that variable isn't used for anything once
the method returns, the change wouldn't affect anything.
I thought ByRef wouldn't and ByVal would, but it looks
like they really mean IN and IN/OUT, so it sounds like I should use
ByVal whenever possible, rather than the other way around.
Yes, stick to ByVal. All parameters are passed by value unless you
specify ByRef, so you can omit the ByVal keyword (unless you want to be
extra obvious somewhere).

When you are passing an object by value, that doesn't mean that the
object is copied. It means that the value of the reference to the object
is copied. Objects are never copied unless you specifically ask for it.
And would
combining this with the function Göran mentioned result in 4
instances?
No.
I sound nit-picky but the CurrentUser object is growing
quickly to incorporate a lot of business logic and I'm still trying to
get my arms around all this!

Brian

Jun 13 '06 #7
Goran,

You have a datapart in your sample as an object holding information.
(You even test if it exist and that is the base of my previous message)

Without that it would not be a problem.

Moreover, without that data part I use it too.

(And for real sharing data between users in an aspnet application instead of
a cache)

Cor

"Göran Andersson" <gu***@guffa.com> schreef in bericht
news:%2***************@TK2MSFTNGP05.phx.gbl...
You are right about using shared classes in ASP.NET, but I'm not talking
about shared classes at all. The code that I'm showing is a factory
method, just as I mentioned in the post.

If you aren't familiar with the concept, a factory method is used to get
an instance of a class instead of directly using the constructor. A
factory method is usually a shared method, so that you don't have to
create a dummy instance of the class just to be able to call the method.
Cor Ligthert [MVP] wrote:
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


Jun 14 '06 #8
A datapart? What do you mean?

I have search for the term "datapart", and the only remotely related I
found was the usage as "a part of some data".

Are you referring to the local variable that is used in the method?

That is a local variable that is declared for the method. The scope of
the variable is only inside the method, and the lifetime of the variable
is only the duration of the call to the method. When the method returns,
the variable no longer exists.

It's not a shared variable that is stored in the class, if that is what
you are thinking of. Notice the lack of the "shared" keyword in the
declaration of the variable.
Cor Ligthert [MVP] wrote:
Goran,

You have a datapart in your sample as an object holding information.
(You even test if it exist and that is the base of my previous message)

Without that it would not be a problem.

Moreover, without that data part I use it too.

(And for real sharing data between users in an aspnet application instead of
a cache)

Cor

"Göran Andersson" <gu***@guffa.com> schreef in bericht
news:%2***************@TK2MSFTNGP05.phx.gbl...
You are right about using shared classes in ASP.NET, but I'm not talking
about shared classes at all. The code that I'm showing is a factory
method, just as I mentioned in the post.

If you aren't familiar with the concept, a factory method is used to get
an instance of a class instead of directly using the constructor. A
factory method is usually a shared method, so that you don't have to
create a dummy instance of the class just to be able to call the method.
Cor Ligthert [MVP] wrote:
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

Jun 14 '06 #9
Goran,

You are right, I had not good looked at your code the data part of your
method is a local variable.
And accoording to the idea of it, it could work.

However I have never seen the by you used statemens as
:Public Shared Sub GetCurrent() As CurrentUser
As well do I not understand how a shared Function which belong to all users
can use a Session which belong to one user.

I had seen your code as a kind of pseudo but it seems to be reality and in
my idea it will throw a lot of errors and never function this way.

But I keep it by this.

Cor
"Göran Andersson" <gu***@guffa.com> schreef in bericht
news:Og**************@TK2MSFTNGP03.phx.gbl...A datapart? What do you mean?

I have search for the term "datapart", and the only remotely related I
found was the usage as "a part of some data".

Are you referring to the local variable that is used in the method?

That is a local variable that is declared for the method. The scope of the
variable is only inside the method, and the lifetime of the variable is
only the duration of the call to the method. When the method returns, the
variable no longer exists.

It's not a shared variable that is stored in the class, if that is what
you are thinking of. Notice the lack of the "shared" keyword in the
declaration of the variable.
Cor Ligthert [MVP] wrote:
Goran,

You have a datapart in your sample as an object holding information.
(You even test if it exist and that is the base of my previous message)

Without that it would not be a problem.

Moreover, without that data part I use it too.

(And for real sharing data between users in an aspnet application instead
of a cache)

Cor

"Göran Andersson" <gu***@guffa.com> schreef in bericht
news:%2***************@TK2MSFTNGP05.phx.gbl...
You are right about using shared classes in ASP.NET, but I'm not talking
about shared classes at all. The code that I'm showing is a factory
method, just as I mentioned in the post.

If you aren't familiar with the concept, a factory method is used to get
an instance of a class instead of directly using the constructor. A
factory method is usually a shared method, so that you don't have to
create a dummy instance of the class just to be able to call the method.
Cor Ligthert [MVP] wrote:
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

Jun 14 '06 #10
A huge thank you again to Cor and Göran! I greatly appreciate the
knowledge you shared!

Cor, I've used a lot of functions that reference the session variable
using httpcontext.current.session. In the past I've had to create a
dummy instance of the "function library" class to get to them like
Göran described above. But I plan to try the public shared
subroutines mentioned, so that may have just changed! :)

Brian

Jun 14 '06 #11
Cor Ligthert [MVP] wrote:
Goran,

You are right, I had not good looked at your code the data part of your
method is a local variable.
And accoording to the idea of it, it could work.

However I have never seen the by you used statemens as
:Public Shared Sub GetCurrent() As CurrentUser
As well do I not understand how a shared Function which belong to all users
can use a Session which belong to one user.


That is not a problem. The call to the method is still specific for the
user.

Actually, the shared keyword for methods doesn't really have anything to
do with sharing the method between users. As a method only is loaded
once into memory, the code for a method is always shared by all users,
regardless if the method is declared as shared or not.

The shared keyword for a method only means that the method is called
without the use of an instance of the class.

The "shared" keyword in VB is somewhat confusing when used this way.
Consider that other languages uses the "static" keyword instead, which
makes more sense in this context.
I had seen your code as a kind of pseudo but it seems to be reality and in
my idea it will throw a lot of errors and never function this way.
No, there is nothing wrong with the code. I assure you that it works
just fine.
But I keep it by this.

Cor
"Göran Andersson" <gu***@guffa.com> schreef in bericht
news:Og**************@TK2MSFTNGP03.phx.gbl...
A datapart? What do you mean?

I have search for the term "datapart", and the only remotely related I
found was the usage as "a part of some data".

Are you referring to the local variable that is used in the method?

That is a local variable that is declared for the method. The scope of the
variable is only inside the method, and the lifetime of the variable is
only the duration of the call to the method. When the method returns, the
variable no longer exists.

It's not a shared variable that is stored in the class, if that is what
you are thinking of. Notice the lack of the "shared" keyword in the
declaration of the variable.
Cor Ligthert [MVP] wrote:
Goran,

You have a datapart in your sample as an object holding information.
(You even test if it exist and that is the base of my previous message)

Without that it would not be a problem.

Moreover, without that data part I use it too.

(And for real sharing data between users in an aspnet application instead
of a cache)

Cor

"Göran Andersson" <gu***@guffa.com> schreef in bericht
news:%2***************@TK2MSFTNGP05.phx.gbl...
You are right about using shared classes in ASP.NET, but I'm not talking
about shared classes at all. The code that I'm showing is a factory
method, just as I mentioned in the post.

If you aren't familiar with the concept, a factory method is used to get
an instance of a class instead of directly using the constructor. A
factory method is usually a shared method, so that you don't have to
create a dummy instance of the class just to be able to call the method.
Cor Ligthert [MVP] wrote:
> 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
>

Jun 14 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Uwe Mayer | last post by:
Hi, sorry for the lack of source code. Here again: I use struct.unpack() to unpack data from a binary file and pass the returned tuple as parameter to __init__ of a class that's supposed to...
28
by: Daniel | last post by:
Hello =) I have an object which contains a method that should execute every x ms. I can use setInterval inside the object construct like this - self.setInterval('ObjectName.methodName()',...
11
by: C++fan | last post by:
Suppose that I define the following class: class example_class{ public: example_class(); void funtion_1(); void function_2(); protected:
2
by: Ryan Mitchley | last post by:
Hi all I have code for an object factory, heavily based on an article by Jim Hyslop (although I've made minor modifications). The factory was working fine using g++, but since switching to the...
16
by: sneill | last post by:
How is it possible to take the value of a variable (in this case, MODE_CREATE, MODE_UPDATE, etc) and use that as an object property name? In the following example I want 'oIcon' object to have...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
4
by: Kevin Frey | last post by:
Can someone please tell me if there is any way to do this easily in C#: x = new MyClass( ); y = new MyClass( ); I want to copy the values from y to x, without it resulting in the construction...
2
by: Ralph | last post by:
Hi I don't understand why it's not working: function schedule(imTop){ this.tdImagesTop = imTop; } schedule.prototype.selectEl = function() { alert(this.tdImagesTop);
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.