View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Patrick Molloy Patrick Molloy is offline
external usenet poster
 
Posts: 1,049
Default Understanding the difference

in older versions of BASIC

Dim sums, colors, products As New Collection
was good enough to declare each variable as a Collection. BUT not now

so this
Dim sums, colors, products As New Collection
is akin to this
Dim sums As Variant
Dim colors As Variant
Dim products As New Collection


Also, using NEW this is not like by many developers as we can't predict when
the object is created.

using
Dim products As Collection

then
SET products = New Collection
leave your code in control




"Fabian" wrote in message
...
Below are two programs that - to my understanding - are the same. Why does
the first one generate an error message and the second one not? Such small
differences (there is a similar thing with the Collection.add function)
often
incur hours of debugging to me and therefore I would love to understand
them.

1st:

Dim sums As New Collection
Dim colors As New Collection
Dim products As New Collection

If ExistsInColl(sums, color) Then "do stuff"

2nd:

Dim sums, colors, products As New Collection

If ExistsInColl(sums, color) Then "do stuff"


the function that is called:

Public Function ExistsInColl(ByRef col As Collection, ByVal index As
String)
As Boolean
On Error GoTo ErrNotInColl
Dim tmp
tmp = col(index)
ExistsInColl = True
Exit Function
ErrNotInColl:
On Error GoTo 0
ExistsInColl = False
End Function