ArrayList Session Objects

Discussion in 'ASP .Net' started by tma, Aug 5, 2004.

  1. tma

    tma Guest

    I'm trying to save code to a session object like the following:

    dim oAppList as arraylist
    dim oApp as someclass

    Code to manipulate oApp...
    ....
    ....
    oApplist.Add(oApp)
    session.add("AppList", oApplist)

    More code to make changes to oApp...
    ....
    ....
    oApplist.add(oApp) 'new version with changes
    session.add("AppList", oApplist) 'save new arraylist in session state

    In the final processing, I do the following:

    oApplist = session("AppList")
    for each oApp in oApplist
    .....
    next oApp

    My problem comes in when I try to iterate through the array list. All the
    items (oApp) in the array have the properties of the last item I added to
    the array list. I cannot seem to figure where I'm going wrong.
    tma, Aug 5, 2004
    #1
    1. Advertising

  2. Fred joins Club 1.
    Fred changes his clothes.
    Fred joins Club 2.
    Fred changes his hair style.
    Fred joins Club 3.

    How is Fred dressed in Club 1?
    How is Fred dressed in Club2?

    Obviously, Fred is dressed the same, no matter what Club you are viewing him
    in.

    Add a class to an Arraylist.
    Change the properties of the Class.
    Add the same class to the Arraylist.
    Change the properties of the Class.
    Add the same class to the Arraylist.

    In what state is the class in ArrayList(0)?
    In what state is the class in ArrayList(1)?

    Obviously, it's the same class, so it is in the state it was in when you
    last changed it.

    Now, if each time through the loop, you start with

    oAppList = New ArrayList()

    Now you're talking about putting a different (new) class in the ArrayList.
    oAppList is simply a container, and it contains whatever class you put into
    it. If you use the same class over and over again, that's what you get.

    --
    HTH,
    Kevin Spencer
    ..Net Developer
    Microsoft MVP
    Big things are made up
    of lots of little things.

    "tma" <> wrote in message
    news:#...
    > I'm trying to save code to a session object like the following:
    >
    > dim oAppList as arraylist
    > dim oApp as someclass
    >
    > Code to manipulate oApp...
    > ...
    > ...
    > oApplist.Add(oApp)
    > session.add("AppList", oApplist)
    >
    > More code to make changes to oApp...
    > ...
    > ...
    > oApplist.add(oApp) 'new version with changes
    > session.add("AppList", oApplist) 'save new arraylist in session state
    >
    > In the final processing, I do the following:
    >
    > oApplist = session("AppList")
    > for each oApp in oApplist
    > ....
    > next oApp
    >
    > My problem comes in when I try to iterate through the array list. All the
    > items (oApp) in the array have the properties of the last item I added to
    > the array list. I cannot seem to figure where I'm going wrong.
    >
    >
    >
    >
    >
    Kevin Spencer, Aug 5, 2004
    #2
    1. Advertising

  3. tma

    tma Guest

    Thank you for the most excellent illustration. A follow-up if you don't
    mind...

    I can instantiate a new class (oApp) and add it to the container (oApplist)?
    My container will then have 3 new classes?

    oApp.Name = Joe
    oAppList.Add(oApp)

    oApp = new classWhatever
    oApp.Name = Susie
    oAppList.Add(oApp)

    oApp = new classWhatever
    oApp.Name = Tom
    oAppList.Add(oApp)

    Will a "For each oApp in oAppList" give me Joe, Susie and Tom? Or all Tom?



    "Kevin Spencer" <> wrote in message
    news:...
    > Fred joins Club 1.
    > Fred changes his clothes.
    > Fred joins Club 2.
    > Fred changes his hair style.
    > Fred joins Club 3.
    >
    > How is Fred dressed in Club 1?
    > How is Fred dressed in Club2?
    >
    > Obviously, Fred is dressed the same, no matter what Club you are viewing

    him
    > in.
    >
    > Add a class to an Arraylist.
    > Change the properties of the Class.
    > Add the same class to the Arraylist.
    > Change the properties of the Class.
    > Add the same class to the Arraylist.
    >
    > In what state is the class in ArrayList(0)?
    > In what state is the class in ArrayList(1)?
    >
    > Obviously, it's the same class, so it is in the state it was in when you
    > last changed it.
    >
    > Now, if each time through the loop, you start with
    >
    > oAppList = New ArrayList()
    >
    > Now you're talking about putting a different (new) class in the ArrayList.
    > oAppList is simply a container, and it contains whatever class you put

    into
    > it. If you use the same class over and over again, that's what you get.
    >
    > --
    > HTH,
    > Kevin Spencer
    > .Net Developer
    > Microsoft MVP
    > Big things are made up
    > of lots of little things.
    >
    > "tma" <> wrote in message
    > news:#...
    > > I'm trying to save code to a session object like the following:
    > >
    > > dim oAppList as arraylist
    > > dim oApp as someclass
    > >
    > > Code to manipulate oApp...
    > > ...
    > > ...
    > > oApplist.Add(oApp)
    > > session.add("AppList", oApplist)
    > >
    > > More code to make changes to oApp...
    > > ...
    > > ...
    > > oApplist.add(oApp) 'new version with changes
    > > session.add("AppList", oApplist) 'save new arraylist in session state
    > >
    > > In the final processing, I do the following:
    > >
    > > oApplist = session("AppList")
    > > for each oApp in oApplist
    > > ....
    > > next oApp
    > >
    > > My problem comes in when I try to iterate through the array list. All

    the
    > > items (oApp) in the array have the properties of the last item I added

    to
    > > the array list. I cannot seem to figure where I'm going wrong.
    > >
    > >
    > >
    > >
    > >

    >
    >
    tma, Aug 5, 2004
    #3
  4. Think of a variable as a box with a handle on top. The handle is the name of
    the variable. It gives you access to the variable. The box is a certain
    size, depending upon what you're planning to put into it. If you're going to
    put an Integer into it, the size of the box is 32 bits. If you're going to
    put a string into the box, it will be the size of a pointer to a string. If
    you want to put a class into it, it will be the size of a pointer to that
    class. But when you first create the box, it is empty. It has nothing in it.

    Now, you put an Integer, a string, or a class into the box. You refer to the
    value (instance) in the box by the box's name, because that is the handle to
    the box, and the value (instance) it contains. However, although you are
    using the box's name, you are using the value inside it via the box's name.
    Remember that the value contained inside the box doesn't have a name. It has
    a type, but that's all. The box has the name, and gives you access to the
    value.

    So, when you say something like:

    oApp = new classWhatever

    "oApp" is the name of the box. The "=" operator puts the new class instance
    into the box (variable)

    The class instance is newly-created. When you assign an instance of a class
    to a variable, you remove the previous instance from the variable.

    An ArrayList is basically an array of pointers (Well, it's a bit more
    complex than that, but for simplicity's sake I'll stick with that). A
    pointer is a number that indicates an address of something in memory. So,
    when you assign a class to an ArrayList element, you are putting a pointer
    to that class instance in the ArrayList. Therefore, if you create your class
    instance outside of your loop, you are putting pointers to the same class
    instance in each element. When you put your call to the New method for that
    class inside the loop, you are putting a pointer to each new class instance
    into the ArrayList.

    And if you understood all of that, you should know the answer to your
    question without my telling you. Right?

    --
    HTH,
    Kevin Spencer
    ..Net Developer
    Microsoft MVP
    Big things are made up
    of lots of little things.

    "tma" <> wrote in message
    news:...
    > Thank you for the most excellent illustration. A follow-up if you don't
    > mind...
    >
    > I can instantiate a new class (oApp) and add it to the container

    (oApplist)?
    > My container will then have 3 new classes?
    >
    > oApp.Name = Joe
    > oAppList.Add(oApp)
    >
    > oApp = new classWhatever
    > oApp.Name = Susie
    > oAppList.Add(oApp)
    >
    > oApp = new classWhatever
    > oApp.Name = Tom
    > oAppList.Add(oApp)
    >
    > Will a "For each oApp in oAppList" give me Joe, Susie and Tom? Or all Tom?
    >
    >
    >
    > "Kevin Spencer" <> wrote in message
    > news:...
    > > Fred joins Club 1.
    > > Fred changes his clothes.
    > > Fred joins Club 2.
    > > Fred changes his hair style.
    > > Fred joins Club 3.
    > >
    > > How is Fred dressed in Club 1?
    > > How is Fred dressed in Club2?
    > >
    > > Obviously, Fred is dressed the same, no matter what Club you are viewing

    > him
    > > in.
    > >
    > > Add a class to an Arraylist.
    > > Change the properties of the Class.
    > > Add the same class to the Arraylist.
    > > Change the properties of the Class.
    > > Add the same class to the Arraylist.
    > >
    > > In what state is the class in ArrayList(0)?
    > > In what state is the class in ArrayList(1)?
    > >
    > > Obviously, it's the same class, so it is in the state it was in when you
    > > last changed it.
    > >
    > > Now, if each time through the loop, you start with
    > >
    > > oAppList = New ArrayList()
    > >
    > > Now you're talking about putting a different (new) class in the

    ArrayList.
    > > oAppList is simply a container, and it contains whatever class you put

    > into
    > > it. If you use the same class over and over again, that's what you get.
    > >
    > > --
    > > HTH,
    > > Kevin Spencer
    > > .Net Developer
    > > Microsoft MVP
    > > Big things are made up
    > > of lots of little things.
    > >
    > > "tma" <> wrote in message
    > > news:#...
    > > > I'm trying to save code to a session object like the following:
    > > >
    > > > dim oAppList as arraylist
    > > > dim oApp as someclass
    > > >
    > > > Code to manipulate oApp...
    > > > ...
    > > > ...
    > > > oApplist.Add(oApp)
    > > > session.add("AppList", oApplist)
    > > >
    > > > More code to make changes to oApp...
    > > > ...
    > > > ...
    > > > oApplist.add(oApp) 'new version with changes
    > > > session.add("AppList", oApplist) 'save new arraylist in session state
    > > >
    > > > In the final processing, I do the following:
    > > >
    > > > oApplist = session("AppList")
    > > > for each oApp in oApplist
    > > > ....
    > > > next oApp
    > > >
    > > > My problem comes in when I try to iterate through the array list. All

    > the
    > > > items (oApp) in the array have the properties of the last item I added

    > to
    > > > the array list. I cannot seem to figure where I'm going wrong.
    > > >
    > > >
    > > >
    > > >
    > > >

    > >
    > >

    >
    >
    Kevin Spencer, Aug 6, 2004
    #4
  5. tma

    tma Guest

    Thank you for your considerate reply. You have been extremely helpful.

    "Kevin Spencer" <> wrote in message
    news:%...
    > Think of a variable as a box with a handle on top. The handle is the name

    of
    > the variable. It gives you access to the variable. The box is a certain
    > size, depending upon what you're planning to put into it. If you're going

    to
    > put an Integer into it, the size of the box is 32 bits. If you're going to
    > put a string into the box, it will be the size of a pointer to a string.

    If
    > you want to put a class into it, it will be the size of a pointer to that
    > class. But when you first create the box, it is empty. It has nothing in

    it.
    >
    > Now, you put an Integer, a string, or a class into the box. You refer to

    the
    > value (instance) in the box by the box's name, because that is the handle

    to
    > the box, and the value (instance) it contains. However, although you are
    > using the box's name, you are using the value inside it via the box's

    name.
    > Remember that the value contained inside the box doesn't have a name. It

    has
    > a type, but that's all. The box has the name, and gives you access to the
    > value.
    >
    > So, when you say something like:
    >
    > oApp = new classWhatever
    >
    > "oApp" is the name of the box. The "=" operator puts the new class

    instance
    > into the box (variable)
    >
    > The class instance is newly-created. When you assign an instance of a

    class
    > to a variable, you remove the previous instance from the variable.
    >
    > An ArrayList is basically an array of pointers (Well, it's a bit more
    > complex than that, but for simplicity's sake I'll stick with that). A
    > pointer is a number that indicates an address of something in memory. So,
    > when you assign a class to an ArrayList element, you are putting a pointer
    > to that class instance in the ArrayList. Therefore, if you create your

    class
    > instance outside of your loop, you are putting pointers to the same class
    > instance in each element. When you put your call to the New method for

    that
    > class inside the loop, you are putting a pointer to each new class

    instance
    > into the ArrayList.
    >
    > And if you understood all of that, you should know the answer to your
    > question without my telling you. Right?
    >
    > --
    > HTH,
    > Kevin Spencer
    > .Net Developer
    > Microsoft MVP
    > Big things are made up
    > of lots of little things.
    >
    > "tma" <> wrote in message
    > news:...
    > > Thank you for the most excellent illustration. A follow-up if you don't
    > > mind...
    > >
    > > I can instantiate a new class (oApp) and add it to the container

    > (oApplist)?
    > > My container will then have 3 new classes?
    > >
    > > oApp.Name = Joe
    > > oAppList.Add(oApp)
    > >
    > > oApp = new classWhatever
    > > oApp.Name = Susie
    > > oAppList.Add(oApp)
    > >
    > > oApp = new classWhatever
    > > oApp.Name = Tom
    > > oAppList.Add(oApp)
    > >
    > > Will a "For each oApp in oAppList" give me Joe, Susie and Tom? Or all

    Tom?
    > >
    > >
    > >
    > > "Kevin Spencer" <> wrote in message
    > > news:...
    > > > Fred joins Club 1.
    > > > Fred changes his clothes.
    > > > Fred joins Club 2.
    > > > Fred changes his hair style.
    > > > Fred joins Club 3.
    > > >
    > > > How is Fred dressed in Club 1?
    > > > How is Fred dressed in Club2?
    > > >
    > > > Obviously, Fred is dressed the same, no matter what Club you are

    viewing
    > > him
    > > > in.
    > > >
    > > > Add a class to an Arraylist.
    > > > Change the properties of the Class.
    > > > Add the same class to the Arraylist.
    > > > Change the properties of the Class.
    > > > Add the same class to the Arraylist.
    > > >
    > > > In what state is the class in ArrayList(0)?
    > > > In what state is the class in ArrayList(1)?
    > > >
    > > > Obviously, it's the same class, so it is in the state it was in when

    you
    > > > last changed it.
    > > >
    > > > Now, if each time through the loop, you start with
    > > >
    > > > oAppList = New ArrayList()
    > > >
    > > > Now you're talking about putting a different (new) class in the

    > ArrayList.
    > > > oAppList is simply a container, and it contains whatever class you put

    > > into
    > > > it. If you use the same class over and over again, that's what you

    get.
    > > >
    > > > --
    > > > HTH,
    > > > Kevin Spencer
    > > > .Net Developer
    > > > Microsoft MVP
    > > > Big things are made up
    > > > of lots of little things.
    > > >
    > > > "tma" <> wrote in message
    > > > news:#...
    > > > > I'm trying to save code to a session object like the following:
    > > > >
    > > > > dim oAppList as arraylist
    > > > > dim oApp as someclass
    > > > >
    > > > > Code to manipulate oApp...
    > > > > ...
    > > > > ...
    > > > > oApplist.Add(oApp)
    > > > > session.add("AppList", oApplist)
    > > > >
    > > > > More code to make changes to oApp...
    > > > > ...
    > > > > ...
    > > > > oApplist.add(oApp) 'new version with changes
    > > > > session.add("AppList", oApplist) 'save new arraylist in session

    state
    > > > >
    > > > > In the final processing, I do the following:
    > > > >
    > > > > oApplist = session("AppList")
    > > > > for each oApp in oApplist
    > > > > ....
    > > > > next oApp
    > > > >
    > > > > My problem comes in when I try to iterate through the array list.

    All
    > > the
    > > > > items (oApp) in the array have the properties of the last item I

    added
    > > to
    > > > > the array list. I cannot seem to figure where I'm going wrong.
    > > > >
    > > > >
    > > > >
    > > > >
    > > > >
    > > >
    > > >

    > >
    > >

    >
    >
    tma, Aug 6, 2004
    #5
    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. Saravanan Rathinavelu

    Iterate through ArrayList using an another ArrayList

    Saravanan Rathinavelu, Aug 16, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    2,726
    Natty Gur
    Aug 19, 2003
  2. Kaidi
    Replies:
    4
    Views:
    2,332
    Kaidi
    Jan 3, 2004
  3. 7stud
    Replies:
    11
    Views:
    668
    Dennis Lee Bieber
    Mar 20, 2007
  4. xz
    Replies:
    16
    Views:
    2,342
  5. Philipp
    Replies:
    6
    Views:
    901
    Arne Vajhøj
    May 28, 2008
Loading...

Share This Page