Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ArrayList Session Objects

Reply
Thread Tools

ArrayList Session Objects

 
 
tma
Guest
Posts: n/a
 
      08-05-2004
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.





 
Reply With Quote
 
 
 
 
Kevin Spencer
Guest
Posts: n/a
 
      08-05-2004
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" <(E-Mail Removed)> wrote in message
news:#(E-Mail Removed)...
> 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.
>
>
>
>
>



 
Reply With Quote
 
 
 
 
tma
Guest
Posts: n/a
 
      08-05-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:#(E-Mail Removed)...
> > 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.
> >
> >
> >
> >
> >

>
>



 
Reply With Quote
 
Kevin Spencer
Guest
Posts: n/a
 
      08-06-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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" <(E-Mail Removed)> wrote in message
> > news:#(E-Mail Removed)...
> > > 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.
> > >
> > >
> > >
> > >
> > >

> >
> >

>
>



 
Reply With Quote
 
tma
Guest
Posts: n/a
 
      08-06-2004
Thank you for your considerate reply. You have been extremely helpful.

"Kevin Spencer" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed)...
> > > 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" <(E-Mail Removed)> wrote in message
> > > news:#(E-Mail Removed)...
> > > > 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.
> > > >
> > > >
> > > >
> > > >
> > > >
> > >
> > >

> >
> >

>
>



 
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
Does the clone() method of ArrayList<> make a copy of the objects in the ArrayList? xz Java 16 08-04-2007 10:33 PM
Unable to serialize the session state. Please note that non-serializable objects or MarshalByRef objects are not permitted when session state mode is 'StateServer' or 'SQLServer'. Mike Larkin ASP .Net 1 05-23-2005 12:33 PM
a class inherited from ArrayList, is saved to ViewState, why the type of the object read from ViewSate is not the class, but the parent, ArrayList leal ting ASP .Net 1 02-10-2004 07:45 PM
writeObject with ArrayList of ArrayList? Kaidi Java 4 01-03-2004 08:16 PM
Iterate through ArrayList using an another ArrayList Saravanan Rathinavelu ASP .Net 3 08-19-2003 07:03 AM



Advertisments