ArrayList - Newbie Misunderstanding

M

mosscliffe

I thought I was getting the hang of all this VB and OOP etc, so I
thought I would test ArrayList and Session Variables.

I defined a Class of three variables and added values to them.
I looped 5 times and added the class to an arraylist
I added the arraylist to a session variable and then read it back into
a new array
It said there were 5 entries and the 'foreach' loops five times, but it
always gives me the values of the last class object added

Is there some indexing I have missed ?

Its been a hard day and if I can crack this, it will have been a very
productive one.

Thanks for looking.

DebugPrint is my adding to a Label routine to see what is going on, as
I have not fully got to grips with Watch etc

Public Class clCategory
Public CatID As Integer
Public CatName As String
Public CatDesc As String
End Class

public sub MyCats
'Create ArrayList

Dim fred As New clCategory
Dim arrCats As New ArrayList
Dim x As Integer
For x = 1 To 5
fred.CatID = x
fred.CatName = "CAT" & x.ToString
fred.CatDesc = "Cat Desc:" & x.ToString & x.ToString
arrCats.Add(fred)
'DebugPrint("arrayCats", "Count", arrCats.Count)
Next
Session("SelCats") = arrCats

'Retrieve ArrayList

Dim sessCats As ArrayList
sessCats = Session("SelCats")
DebugPrint("arrayCats", "Count", sessCats.Count)
For Each tim As clCategory In sessCats
DebugPrint("Cat", "CatID", tim.CatID)
DebugPrint("Cat", "CatName", tim.CatName)
DebugPrint("Cat", "CatDesc", tim.CatDesc)

Next
End Sub
 
S

sdbillsfan

You're thinking that you're passing a copy of the object to the
arraylist when you're actually passing the reference. You end up with 5
references to your fred object, which will of course all have the last
property values set in your loop.
 
M

mosscliffe

Thanks for that info.

Is it possible you or someone could help with how I pass the object as
opposed to the reference , I am not grasping the difference.
 
M

mosscliffe

Thanks for the info, but I prefer to stay a VB man. I never did get the
point of VOID. If there is an equivalent VB book, then please let me
know, I do have the WROX Professional books on ASP and VB.

All I want is a simple example in VB preferably, of how I add
arraylists to an ArrayList in a Session(var) so that I can persist
those values throughout my site.

Thanks - Richard
 
R

Ray Booysen

The misunderstanding here is that you're not adding physical objects to
the arraylist. But references to the object.

Your code:

Dim fred As New clCategory
Dim arrCats As New ArrayList
Dim x As Integer
For x = 1 To 5
fred.CatID = x
fred.CatName = "CAT" & x.ToString
fred.CatDesc = "Cat Desc:" & x.ToString & x.ToString
arrCats.Add(fred)
'DebugPrint("arrayCats", "Count", arrCats.Count)
Next
Session("SelCats") = arrCats

You declare fred as a new clCategory and then loop. In your loop, what
is actually happening is that you are just setting and resetting the
properties for the same fred object and adding multiple references to
the same fred object to the arraylist.

To get your code to work, you would need to place the DIM statement of
fred inside your loop. This ensures unique entities. Maybe use a
better naming scheme as well? :p

Regards
Ray
 
M

mosscliffe

Ray,

Many thanks. I see now I was adding five references to the same
variable.
I need to create a new instance of the class on each iteration. So my
Dim for the class is moved inside the Loop and on each iteration
creates a NEW instance of the Class Object.

I did that and it works as I intended.

I had not realised Session vars are only pointers or references, to the
actual Variables. I had assumed it was a Stack, which loaded up the
contents of the variables.

I think I was influenced in my idea, because I had assumed, that each
page, had a fresh memory area.

I will get there, I hope, eventually.

Thanks again all - for your assistance, when you are working alone, it
is nice to get a little encouragement and help from time to time

Richard
 
G

Guest

No prob if you're not interested in C#. However, what you're dealing with
here is a conceptual thing, not a syntax thing, and the concept is
consistent across many programming languages. The book is useful whether you
want to use VB or C# or Java.

-K F
 
M

mosscliffe

Thanks again for your reply.

Maybe I am very niave or old fashioned, but how is an object any
different from a subroutine with multiple entry and exit points - in
other words a subprogram with a giant case statement at the beginning,
determining which bit is accessed and which bit is returned. This
subprogram can either be local or global.

I have been bamboozled for years with terms such as polymorphism etc,
but i have never seen how the end result is any different from my
interpretation.

I can appreciate how the concept, helps with error free coding, but I
can not see how the concept, is any different from calling subroutines,
which may or may not return some value or modify a value passed as a
reference.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top