ArrayList Session Objects

T

tma

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.
 
K

Kevin Spencer

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.
 
T

tma

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?
 
K

Kevin Spencer

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top