473,406 Members | 2,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

ArrayList Session Objects

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.

Nov 18 '05 #1
4 2445
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" <tm*@allisconfusing.net> wrote in message
news:#t**************@TK2MSFTNGP10.phx.gbl...
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.


Nov 18 '05 #2
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?

"Kevin Spencer" <ks******@takempis.com> wrote in message
news:OJ**************@TK2MSFTNGP10.phx.gbl...
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" <tm*@allisconfusing.net> wrote in message
news:#t**************@TK2MSFTNGP10.phx.gbl...
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.



Nov 18 '05 #3
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" <tm*@allisconfusing.net> wrote in message
news:OB**************@TK2MSFTNGP10.phx.gbl...
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" <ks******@takempis.com> wrote in message
news:OJ**************@TK2MSFTNGP10.phx.gbl...
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" <tm*@allisconfusing.net> wrote in message
news:#t**************@TK2MSFTNGP10.phx.gbl...
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.




Nov 18 '05 #4
tma
Thank you for your considerate reply. You have been extremely helpful.

"Kevin Spencer" <ks******@takempis.com> wrote in message
news:%2***************@tk2msftngp13.phx.gbl...
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" <tm*@allisconfusing.net> wrote in message
news:OB**************@TK2MSFTNGP10.phx.gbl...
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" <ks******@takempis.com> wrote in message
news:OJ**************@TK2MSFTNGP10.phx.gbl...
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" <tm*@allisconfusing.net> wrote in message
news:#t**************@TK2MSFTNGP10.phx.gbl...
> 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.
>
>
>
>
>



Nov 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Omer | last post by:
Hi All, We are trying to invoke EJB methods on simple stateless session beans from an Applet running remotely. One of our methods needs to return an ArrayList of Serializable objects. We...
3
by: Celine | last post by:
is it possible to pass an arraylist from one page to another page? If yes how? (I'm using asp.net and c#)
2
by: Steven | last post by:
Hello, In my asp.net application, I have the MainForm.aspx.cs (where all the functions are defined) and ClasssificationInfo.cs class. This is the Classification Info class -- using System;...
7
by: simon | last post by:
First, I create arrayList and put it into a session. (should I use arrayList to store different types of data into collection or should I use something else?) ArrayList arrayValues = new...
7
by: Dave | last post by:
Hi all, I have... using System.Collections; namespace MyApp.Templates { public class MyPage : System.Web.UI.Page { ArrayList MyArray = new ArrayList();
6
by: fniles | last post by:
I am using VB.NET 2003 and a socket control to receive and sending data to clients. As I receive data in 1 thread, I put it into an arraylist, and then I remove the data from arraylist and send it...
2
by: pargat.singh | last post by:
Hi Everyone: I am using ArrayList to store my objects. Currently i am using session to store this arraylist and when i open showModalDialog i reterive this value from session and assign to...
4
by: =?Utf-8?B?YmFzdWxhc3o=?= | last post by:
Hi; I want to store a datatable (or an arraylist) as a session variable but when I try; Session = al_RecNo; I get an error that; "Cannot implicitly convert type...
10
by: writeallnight | last post by:
I can't seem to "disconnect" a "rebuilt" ArrayList of objects from it's source AL; I thought I understood the rules on copying AL Data objects vs. the "clone" Method (and it's simply being a...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.