Declaring & Initializing Classes & ArrayLists

G

GrandpaB

While writing this plea for help, I think I solved my
dilemma, but I don't know why the statement that solved
the problem 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, MyArt and ArtList had been
declared with the following statements:

Public ArtList as New ArrayList
Public MyArt as New Art
'Art is a class that contains 6 Strings & 1 Integer

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
class. In the application I want to add, delete and
edit the individual Art objects in ArtList. I can
sucessfully edit the Art objects by transferring a
selected object from the ArtList to MyArt and then to the
application's Form with the following code.

MyArt = ArtList(Index)
MyArt2Form()

Then after changes have been made to the form, I transfer
the edited information from the Form, to MyArt and
finally back to ArtList with the following code:

Form2MyArt()
ArtList(Index) = MyArt

However, in the case where I want to add a new object to
the ArtList, why I must reinitialize MyArt with the
following statement?

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

Thanks, GrandpaB
 
C

Cor Ligthert

Grandpa,

Did you see my answer in your origial thread accoording this question, you
did not reply on that.

Accoording too that you see that you can set an reference too an object
global as you did

When you do that as you did, than you tell with this
MyArt = ArtList(Index)
MyArt is after this referencing to the same place on the heap as
ArtList(index) which references an item in the collection of ArtList
Form2MyArt()
ArtList(Index) = MyArt

And here you tell set ArtList(Index) to the adress of that MyArt (a little
bit useles when it is the same)
However, in the case where I want to add a new object to
the ArtList, why I must reinitialize MyArt with the
following statement?

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

Here you create a new object and add the reference of that too the ArtList
item collection.

However I explained that more in dept my answer in the original thread with
your question.

I hope this helps?

Cor
 
M

Mike McIntyre

GrandpaB,
MyArt = New Art ' **** ??????? ****

That statement declares a variable named MyArt of type Art, creates a new
Art object in heap memory, and assigns a reference to the new Art object to
the MyArt variable. The end result is that MyArt now refers to a new Art
object.

In the example it is used to create a new Art object that is then added to
the ArtList ArrayList.

You may find this article helpful:
http://www.devcity.net/net/article.aspx?cid=10&y=2004&m=1&d=22


--
Mike

Mike McIntyre
Visual Basic MVP
www.getdotnetcode.com
 
L

Larry Serflaten

GrandpaB said:
While writing this plea for help, I think I solved my
dilemma, but I don't know why the statement that solved
the problem is necessary.

This is the third identical post from you in 2 days, why the redundancy?

LFS
 
G

Guest

LFS,
Part of the reason for the multiple posts was confusion
on using the Newsgroups fourms. The top right window
lists the forum, but the bottom window could refereence a
different fourm. Then the search in the bottom window
searches the fourm to which it is associated, not the one
listed in the top window unless you carefully change the
forum on the search form. Thus my original post seemed
to disapear, and it was corrected by a second post to a
different fourm. The second fourm was unwittingly a VB
fourm not a VB.Net fourm. Then by this time I felt that
the problem was more abbout declaring and instaintating
variables and classes than about ListArrays so it was
refocused and resubmitted. Sorry for the long response
and confusion.

GrandpaB
 
G

Guest

Cor,
No, I did not; I got lost using the Microsoft Newsgroup
forms as explained in my response to Larry.

I am just learning about Heaps & Stacks, so please bear
with me. The application can edit many art objects after
I delcare ArtList and MyArt as shown below without having
to reinstaintate MyArt.

Public ArtList as New ArrayList
Public MyArt as New Art
'Art is a class that contains 6 Strings & 1 Integer

As I have previously said the two statements:

MyArt = ArtList(Index)
ArtList(Index) = MyArt

allow me to move data back and forth between MyArt and
ArtList(Index), changing Index each time without having
to reinstaintate MyArt.

The two subroutines:

MyArt2Form()
Form2MyArt()

allow me to transfer databack and forth as many times as
I want between Form1 and MyArt without having to
reinstaintate MyArt.

I have used MyArt as temporary storage to to hold the
data while transferring it back and forth between Form1
and ArtList.

I guess I could attempt to eliminate MyArt and transfer
directly between Form1 and the ArtList. This is getting
off my original question, but could this work:

ArtList(Index).Title = tbTitle.Text
ArtList(Index).Description = tbDescription.Text
etc, etc??

Back to the original question, if I want to add a Art
object to the ArtList collection I must first
reinstaintate MyArt as shown below:

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

After the Art object has been added the user can again
edit as many members of ArtList without reinstaintating
MyArt. The only time MyArt needs to be reinstaintated si
before adding more Art to ArtList.

I know that this is probably taking more time than its
worth, but I appreciate the education, thanks

GrandpaB
 
G

GrandpaB

Mike,

Thanks for the URL, It was a great article, I am gaining
an appreciation for Stacks and Heaps. So far,I have been
unable to find Parts 2 & 3 to that article. As far as it
went, the article did not explain why I needed to
reinstantiate MyArt before I used the ArtList.Add(MyArt)
method. MyArt can be used to transfer data to and from
ArtList many time without reinstantiating MyArt with:

ArtList(Index) = MyArt
MyArt = ArtList(Index)

Thanks again, GrandpaB
 

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