Loading a collection

  • Thread starter Thread starter Scott Cupstid
  • Start date Start date
S

Scott Cupstid

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
 
Scott Cupstid said:
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
///
 
Scott said:
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.
 
Phill W. said:
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
 
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?
 
Scott Cupstid said:
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 said:
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
///

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
 
Exactly as I would do it,

Cor

Herfried K. Wagner said:
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
///
 
Phill W. said:
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.
 
Back
Top