Need basic help with classes

Discussion in 'ASP .Net' started by CarlosSanchezJr@gmail.com, Aug 14, 2006.

  1. Guest

    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.
     
    , Aug 14, 2006
    #1
    1. Advertising

  2. Hi,

    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
     
    Laurent Bugnion, Aug 14, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Psykarrd

    I need some basic C++ help

    Psykarrd, Nov 20, 2003, in forum: C++
    Replies:
    41
    Views:
    1,010
    Gavin Deane
    Nov 23, 2003
  2. Engineer
    Replies:
    6
    Views:
    655
    Jeremy Bowers
    May 1, 2005
  3. Replies:
    0
    Views:
    461
  4. frankie_85
    Replies:
    11
    Views:
    569
    Fredrik Lundh
    Oct 28, 2006
  5. jackie

    a basic question,need help

    jackie, Aug 13, 2008, in forum: C Programming
    Replies:
    14
    Views:
    545
    Antoninus Twink
    Aug 13, 2008
Loading...

Share This Page