473,395 Members | 1,581 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,395 software developers and data experts.

Loading a collection

Ok, here it is. Which is more correct?

AList = New ArrayList
for i = 0 to 10
A = New myObject
AList.Add(A)
A = nothing
next
--- OR ---

AList = New ArrayList
for i = 0 to 10
A = New myObject
AList.Add(A)
next
Mar 13 '07 #1
9 1133
"Scott Cupstid" <sc******@tampabay.rr.comschrieb:
Ok, here it is. Which is more correct?

AList = New ArrayList
for i = 0 to 10
A = New myObject
AList.Add(A)
A = nothing
next
--- OR ---

AList = New ArrayList
for i = 0 to 10
A = New myObject
AList.Add(A)
next
You do not need to set 'A' to 'Nothing' if it is a local variable. I'd
write:

\\\
Dim AList As New ArrayList()
For i As Integer = 0 To 10
AList.Add(New MyObject())
Next i
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Mar 13 '07 #2
Scott Cupstid wrote:
AList = New ArrayList
for i = 0 to 10
A = New myObject
AList.Add(A)
A = nothing
next
--- OR ---
AList = New ArrayList
for i = 0 to 10
A = New myObject
AList.Add(A)
next
The latter, since setting object references to Nothing is no longer
required; Garbage Collection will take care of it.

If myObject contains unmanaged resources, then a call to ...

A.Dispose()

.... might be more appropriate, but if not ...

AList = New ArrayList
For i = 0 To 10
AList.Add(New myObject)
Next

.... would do nicely.

HTH,
Phill W.
Mar 13 '07 #3

"Phill W." <p-.-a-.-w-a-r-d-@-o-p-e-n-.-a-c-.-u-kwrote in message
news:et**********@south.jnrs.ja.net...
Scott Cupstid wrote:
>AList = New ArrayList
for i = 0 to 10
A = New myObject
AList.Add(A)
A = nothing
next
>--- OR ---
>AList = New ArrayList
for i = 0 to 10
A = New myObject
AList.Add(A)
next

The latter, since setting object references to Nothing is no longer
required; Garbage Collection will take care of it.

If myObject contains unmanaged resources, then a call to ...

A.Dispose()

... might be more appropriate, but if not ...

AList = New ArrayList
For i = 0 To 10
AList.Add(New myObject)
Next

... would do nicely.

HTH,
Phill W.
I don't think that a dispose is a good idea. What you are doing is creating
an object, then putting a reference to it in the array. If you dispose it
then the object could be in a less that useful state.

Lloyd Sheen

Mar 13 '07 #4
Thanks.

Just trying to get used to VB after doing years of C++ and Delphi.
"Herfried K. Wagner [MVP]" <hi***************@gmx.atwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
"Scott Cupstid" <sc******@tampabay.rr.comschrieb:
>Ok, here it is. Which is more correct?

AList = New ArrayList
for i = 0 to 10
A = New myObject
AList.Add(A)
A = nothing
next
--- OR ---

AList = New ArrayList
for i = 0 to 10
A = New myObject
AList.Add(A)
next

You do not need to set 'A' to 'Nothing' if it is a local variable. I'd
write:

\\\
Dim AList As New ArrayList()
For i As Integer = 0 To 10
AList.Add(New MyObject())
Next i
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Mar 13 '07 #5
Ok, I think this a little more complicated. I have two lists both
referencing the same collection of objects. The two lists simply provide
different traversal paths for the objects. For example, one may be an
alphabetic listing and the other a numeric listing. How would I load each
list (givent that each list has it's own method for sorting) without
actually instantiating multiple instances of the objects?

Dim List1 as New ArrayList()
Dim List2 as New ArrayList()
Dim A as MyObject()
Dim i as Integer

for i = 0 to 10
A = New MyObject()
List1.Add(A)
List2.Add(A)
next

In this case if I access A from one list, will the changes I make to A be
recognized by the same instance of A in the second list? Also, if I set a
reference to A in List1 to nothing, will the reference to the same instance
of A in the second list be invalid?

I guess my real question is does VB handle a reference count automatically
on instances of objects?
"Herfried K. Wagner [MVP]" <hi***************@gmx.atwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
"Scott Cupstid" <sc******@tampabay.rr.comschrieb:
>Ok, here it is. Which is more correct?

AList = New ArrayList
for i = 0 to 10
A = New myObject
AList.Add(A)
A = nothing
next
--- OR ---

AList = New ArrayList
for i = 0 to 10
A = New myObject
AList.Add(A)
next

You do not need to set 'A' to 'Nothing' if it is a local variable. I'd
write:

\\\
Dim AList As New ArrayList()
For i As Integer = 0 To 10
AList.Add(New MyObject())
Next i
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Mar 13 '07 #6

"Scott Cupstid" <sc******@tampabay.rr.comwrote in message
news:eH**************@TK2MSFTNGP03.phx.gbl...
Ok, I think this a little more complicated. I have two lists both
referencing the same collection of objects. The two lists simply provide
different traversal paths for the objects. For example, one may be an
alphabetic listing and the other a numeric listing. How would I load each
list (givent that each list has it's own method for sorting) without
actually instantiating multiple instances of the objects?

Dim List1 as New ArrayList()
Dim List2 as New ArrayList()
Dim A as MyObject()
Dim i as Integer

for i = 0 to 10
A = New MyObject()
List1.Add(A)
List2.Add(A)
next

In this case if I access A from one list, will the changes I make to A be
recognized by the same instance of A in the second list? Also, if I set a
reference to A in List1 to nothing, will the reference to the same
instance of A in the second list be invalid?

I guess my real question is does VB handle a reference count automatically
on instances of objects?
"Herfried K. Wagner [MVP]" <hi***************@gmx.atwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
>"Scott Cupstid" <sc******@tampabay.rr.comschrieb:
>>Ok, here it is. Which is more correct?

AList = New ArrayList
for i = 0 to 10
A = New myObject
AList.Add(A)
A = nothing
next
--- OR ---

AList = New ArrayList
for i = 0 to 10
A = New myObject
AList.Add(A)
next

You do not need to set 'A' to 'Nothing' if it is a local variable. I'd
write:

\\\
Dim AList As New ArrayList()
For i As Integer = 0 To 10
AList.Add(New MyObject())
Next i
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Scott, think of it this way. You create the object once. At that point it
most likely is "resident" in a variable. Now you can add it to as many
collections as you wish. You could later on find an object in a collection
and then add it to another. Since each collection has its own sorting each
list can show the objects in a specific order.

The other good thing -- update the object once. Now if you update info you
should know where it is shown since changing a field may change the display
characteristics of that object.

Lloyd Sheen

Mar 13 '07 #7
Yes
Mar 13 '07 #8
Exactly as I would do it,

Cor

"Herfried K. Wagner [MVP]" <hi***************@gmx.atschreef in bericht
news:%2****************@TK2MSFTNGP06.phx.gbl...
"Scott Cupstid" <sc******@tampabay.rr.comschrieb:
>Ok, here it is. Which is more correct?

AList = New ArrayList
for i = 0 to 10
A = New myObject
AList.Add(A)
A = nothing
next
--- OR ---

AList = New ArrayList
for i = 0 to 10
A = New myObject
AList.Add(A)
next

You do not need to set 'A' to 'Nothing' if it is a local variable. I'd
write:

\\\
Dim AList As New ArrayList()
For i As Integer = 0 To 10
AList.Add(New MyObject())
Next i
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Mar 13 '07 #9
"Phill W." <p-.-a-.-w-a-r-d-@-o-p-e-n-.-a-c-.-u-kschrieb:
>AList = New ArrayList
for i = 0 to 10
A = New myObject
AList.Add(A)
A = nothing
next
>--- OR ---
>AList = New ArrayList
for i = 0 to 10
A = New myObject
AList.Add(A)
next

The latter, since setting object references to Nothing is no longer
required; Garbage Collection will take care of it.

It was not even necessary in VB6 if 'A' was a local variable.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Mar 13 '07 #10

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

Similar topics

5
by: Kurt Bauer | last post by:
I have an ASP group calendar application which pulls calendar data from Exchange via webdav into an XML string. I then loop the XML nodes to populate a collection of appointments. Finally I use...
5
by: Kurt Bauer | last post by:
I have an ASP group calendar application which pulls calendar data from Exchange via webdav into an XML string. I then loop the XML nodes to populate a collection of appointments. Finally I use...
0
by: Phl | last post by:
Hi, I am trying to create an webform which loads usercontrols dyanamically. I know exactly what to load for some of these controls but for some, I dont want to load it until the user has press a...
5
by: =?Utf-8?B?V2FubmFiZQ==?= | last post by:
We have a page that is loading very slow. There is not a lot of data, not a lot of users are connected at the same time and the page does not produce an error, so I am not sure where to start to...
7
by: Rotsey | last post by:
Hi, I am loading a tab control on a form. The code loads textboxes and comboboxes and checkboxes, normal data entry form that loads a table row of data. I have a combo on the form above the...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...

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.