Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / February 2006

Tip: Looking for answers? Try searching our database.

Using "Collection"

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Greg - 06 Feb 2006 14:02 GMT
Wading into new territory here.  When using a collection, how should it
be declared and set?

The below example seems to work, but I was wondering if it is more
appropriate or stable to both declare and set like:
Dim oBMCol as Collection
Set oBMCol = New Collection

Sub LessonCollection1()
Dim i As Integer
Dim ItemCount As Integer
ItemCount = ActiveDocument.Bookmarks.Count
Dim oBMCol As New Collection
For i = 1 To ItemCount
 If InStr(ActiveDocument.Bookmarks(i).Name, "Test") <> 0 Then
   oBMCol.Add "Bookmark " & i & " name: " & _
           ActiveDocument.Bookmarks(i).Name
   End If
Next i
'Now read back
For i = 1 To oBMCol.Count
 MsgBox oBMCol(i)
Next i
End Sub

Maybe neither method is best so I am anxious to hear from an expert.
Jezebel - 06 Feb 2006 17:20 GMT
A lot of programmers (including me) never use 'Dim ... as new' for any kind
of object. Although it's not inherently evil -- and works perfectly well in
your example -- it's a style of programming that tends to spawn bugs. Apart
from anything else, you can never test whether the object is nothing.
Consider --

Dim x as new objecttype

do stuff with x
set x = nothing
if x is nothing then ....            << will never be true, because the
reference re-instantiates the object.

So your first form of declaration is more robust coding.

> Wading into new territory here.  When using a collection, how should it
> be declared and set?
[quoted text clipped - 22 lines]
>
> Maybe neither method is best so I am anxious to hear from an expert.
Greg - 06 Feb 2006 17:34 GMT
Got it.  Thanks Jezebel.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.