ArrayList Help

G

GrandpaB

While writing this plea for help, I think I solved my
dilemma, but I don't know why the problem solving
statement is necessary. The inspiration for the
statement came from an undocumented VB example I found on
the web. I would be most appreciative if someone could
explain why this statement is necessary and what does it
do:

MyArt = New Art

Early in the code, ArtList, MyArt & AddNew had been
declared with the following statements:

Public ArtList as New ArrayList
Public MyArt as New Art
Public AddNew as Boolean

I am creating a Windows application which uses an
ArrayList to contain a small amount of data. ArtList, is
an instance of an ArrayList that will contain multiple
(up to 200) Art objects. MyArt is an instance of the Art
object. In the application I want to add, delete and
edit the individual Art objects in ArtList. I am
serializing and deserializing the ArrayList and using a
FileStream to read and write to the hard drive. This is
the code in question, and the mystery statement is
commented with ******

Private Sub btnNew_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnNew.Click

ClearForm() 'A sub to clear the Form's textboxes
AddNew = True
End Sub

Private Sub btnAdd_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnAdd.Click

Dim Msg1 As String = "Do you want to ADD this new
Object?"
Dim Msg2 As String = "Update Art"
Dim Ans As MsgBoxResult
If FormChanged() And AddNew Then
Ans = MsgBox(Msg1, MsgBoxStyle.YesNo, Msg2)
If Ans = MsgBoxResult.Yes Then
MyArt = New Art '??? ********* ???
Form2MyArt() 'Move textbox entries to MyArt
ArtList.Add(MyArt)
SaveFile() 'Serialize ArtList & write
FileStream
End If
End if
End Sub
 
C

Cor Ligthert

GrandpaB

A try to answer as short as possible.

Public MyArt as New Art
Creates a new object from the class Art and does the sub New in that class

MyArt as New art
Creates a new object from the class Art and does the sub New in that class
The previous MyArt looses his reference and will therefore be deleted by the
Garbage Collector (GC) when that start doing its job.

And not important, an object is forever created on the managed heap.

I hope this helps?

Cor
 
G

Guest

Cor,

Thanks for your assistance; yes, I think that I'm
starting to gain some insight to improve my
understanding, but I still have a puzzlement.

Let me reitterate to see if I understand. My original
statement:

Public MyArt as New Art

created MyArt as a new Art object. With this object I
was able run the subroutine:

Sub DisplayArt(ByVal Index As Integer)
'Transfer the ArtList(Index) to MyArt and then to
the form
MyArt = ArtList(Index)
MyArt2Form()
End Sub

and the statements:

Form2MyArt()
ArtList(Index)=MyArt

to sucessfully edit the various objects in ArtList, but
when I wanted to add a item to ArtList I had to include
the statement:

MyArt = New Art

So why could I use the original MyArt to edit various
items in ArtList, but when I wanted to add an item to
ArtList I needed reinitialize MyArt using the following
statements:

MyArt = New Art '??? **** ???
Form2MyArt
ArtList.Add(MyArt)

Thanks in advance, GrandpaB
 
C

Cor Ligthert

Grandpa

I see it,

When you add that myArt to the arraylist than there is a reference from the
arraylist to it.

Better, it are everytime objects in the arraylist which are referenced and
stay referenced until they are removed from the arraylist or the arraylist
does not exist anymore.

I did not completly look at your code. However probably I would not do it
that way as you do it and use instead of
MyArt = New Art '??? ********* ???

Dim myArt as New art '??? ********* ???

For as far as I can see the code, do I as well not see sense for that
Public MyArt as New Art.

You only need to create objects for in your arraylist from the type of Art.

I hope this helps?

Cor
 
G

GrandpaB

Cor,

Thanks again for the followup. I guess that my question
boils down to: Why can I transfer existing objects, back
and forth, between AtrList(Index) and MyArt after
declaring ArtList and MyArt with:

Public ArtList as New ArrayList
Public MyArt as New Art

but when I want to add a new object to ArtList,
ArtList.Add(MyArt), I must reinitialize MyArt with:

MyArt = New Art

or, as you suggest, with

Dim MyArt as New Art

The code works and that is my first concern, but I am
attempting to deepen my understanding of VB.

Once again thanks, GrandpaB
 
L

Larry Serflaten

GrandpaB said:
The code works and that is my first concern, but I am
attempting to deepen my understanding of VB.

Imagine you want to fill a parking lot with different styles of cars.
So, you grab a new car (that has interchangable parts) and assign
a new color and interior to it, and put it out in the lot. Then you
grab that same car, and assign a new color and interior to it, and
place it out in the lot. Then you grab that same car again, and
assign a third color and and interior to it and place it back out in
the lot.

How many cars are out in the lot?

There is only one, its the same car, you've just given it different
values. What you need to do is grab a new car each time, and
then assign the values. The same thing happens with objects.

If you continually add the same object to a list, then you end
up with a list where all entries point to the same object. In the
end, that object can only have one complete set of values, so
the list appears to be filled with identical objects, when in fact
its the same object, for each entry in the list. What needs to
be done is to create a new object, when a completely different
object is needed. Obviously enough, to create new objects,
the New keyword is required....

LFS
 
C

Cor Ligthert

Grandpa,

When you create a new object you say

Create a new object on the managed heap with all parameters set as build in
or setted in the Sub New and give it a reference in its placeholder for
where it is.

Than you *add* its reference too an arraylist collection of objects.

The arraylist itself is as well an object

You create everytime in your loop a complete new object and add it too that
collection.

At the end when the methode (even directly at every nested part) is ready,
everything created and is on the stack for that will be cleaned up so as
well all references. However your arraylist has in his collection a lot of
references too all those new objects and those will stay because the GC will
see those references.

When you set first
Private myArt as new Art than you create an objecte and you have created
globaly that reference to it in a global placeholder

When you do than
myArt as new Art than you create an object again, and set the reference in
that global placeholder for that last created object.

The reference of the first is therefore completly gone and can (and will)
be deleted by the GC.

However in this case is the last created myArt accesible from that global
placeholder and therefore from everywhere in your class. Therefore I wrote
that I would do it *probably* with dim myArt as new Art.

The reason is that I cannot see if you do something as this

myArt = Art
DoSubFillArt

Where I in that case would do
DoSubFillArt(myArt)

In the first sample directly above, (because you have set the reference of
myArt globaly), you do not need to pass it too that method the last one is
in that global reference, however in my opinion is that not nice
programming.

Is it not simple, I hope this helps?

Cor
 

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

Similar Threads


Top