Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Need basic help with classes

Reply
Thread Tools

Need basic help with classes

 
 
CarlosSanchezJr@gmail.com
Guest
Posts: n/a
 
      08-14-2006
I have a User class that for example's sake looks something like this:

public class User
{
private int userId;
private string userName;
private string FirstName;
private string LastName;

public int UserId
{
get { return userId; }
set { userId = value; }
}
public int UserName
{
get { return userName; }
set { userName = value; }
}
public int FirstName
{
get { return firstName; }
set { firstName = value; }
}
public int LastName
{
get { return lastName; }
set { lastName = value; }
}
}

Now I understand if I want to create an instance of this on a code
behind page, I would do the following:

User user = new User();
user.UserId = 2;
etc....

What I want to know is, once I populate User with data, can I access
that data from a completely different page (than the one which
populated it or created the new instance)? Any help would be greatly
appreciated.

 
Reply With Quote
 
 
 
 
Laurent Bugnion
Guest
Posts: n/a
 
      08-14-2006
Hi,

http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> I have a User class that for example's sake looks something like this:
>
> public class User
> {

<snip>
> }
>
> Now I understand if I want to create an instance of this on a code
> behind page, I would do the following:
>
> User user = new User();
> user.UserId = 2;
> etc....
>
> What I want to know is, once I populate User with data, can I access
> that data from a completely different page (than the one which
> populated it or created the new instance)? Any help would be greatly
> appreciated.


The Page object is created on every request, and deleted every time that
the request is completed. It means that the objects belonging to the
Page must be recreated every time. That's the "stateless" approach.

However, you can save objects between roundtrips and share them between
pages by saving them in the Session. Each Page has a Session property,
which is created when the first request arrives, and which is deleted
only when the session is explicitly terminated, or when the session
timeout occurs (typically 20 minutes after the last request was processed).

To save an instance like you create in the session object, from within
the Page, you simply do

this.Session[ "aLabel" ] = user;

When you retrieve the object, you must cast it:

User user = (User) this.Session[ "aLabel" ];

If this is the first call, then the object will be null.

There are other strategies to persist objects between requests, but this
is probably the most standard one.

HTH,
Laurent
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
TurboTax Basic vs. Taxcut Basic? Sharp Dressed Man Computer Support 1 01-12-2009 12:52 PM
What is the difference between Visual Basic.NET and Visual Basic 6? Jimmy Dean Computer Support 3 07-25-2005 07:05 AM
Re: Python interpreter in Basic or a Python-2-Basic translator. rrr@ronadam.com Python 0 05-02-2005 01:48 PM
Python interpreter in Basic or a Python-2-Basic translator. Engineer Python 6 05-01-2005 10:16 PM
Upgrading Microsoft Visual Basic 6.0 to Microsoft Visual Basic .NET Jaime MCSD 2 09-20-2003 05:16 AM



Advertisments