Adding Objects to Collections 
Author Message
 Adding Objects to Collections

I have a class with an ADODB.Connection object as one of its properties. I
also have a collection object of this class. Yet when I try to add an ado
connection object to the collection I get "Object Variable or with block
variable not set" (Run-time error #91).

Why is this happening and what is the correct way to add objects to
collection classes.

Thanks in advance.
--

Mark J. Brown

Software Engineer
Platinum Software Corporation
http://www.*-*-*.com/

begin 666 Mark J. Brown.vcf
M0D5'24XZ5D-!4D0-"E9%4E-)3TXZ,BXQ#0I..D)R;W=N.TUA<FL[2BX-"D9.






M;F4L($-!(#DR-C$X/3!$/3!!55-!#0I54DPZ#0I54DPZ:'1T<#HO+W=W=RYP
M;&%T<V]F="YC;VT-"D5-04E,.U!2148[24Y415).150Z;6)R;W=N0'!L871S

`
end
end



Sat, 25 Nov 2000 03:00:00 GMT  
 Adding Objects to Collections

You need to declare the collection As New

dim colMyCollection as New Collection

This instantiates the Collection Object.



Sun, 26 Nov 2000 03:00:00 GMT  
 Adding Objects to Collections

Quote:

>You need to declare the collection As New
>dim colMyCollection as New Collection
>This instantiates the Collection Object.

Actually, it doesn't.  The collection is instantiated the first time you
reference it.  The DIM does not create the instance.  Instead, VB has to
check every time you reference colMyCollection to see if it exists or not.
It's generally a better idea to use:

Dim colMyCollection As Collection
Set colMyCollection=New Collection

The advantage is that you control when the collection is created and
destroyed and can test it with "If colMyCollection Is Nothing Then".  Using
"Dim ... As New ..." gives up a lot of control over the objects.  It may
also have a performance hit if VB does actually test the instance on every
reference (instead of trapping the error if you reference it without
creating it).



Sun, 26 Nov 2000 03:00:00 GMT  
 Adding Objects to Collections

Thanks, I figured it out.

--

Mark J. Brown

Software Engineer
Platinum Software Corporation
http://www.platsoft.com

Quote:

>You need to declare the collection As New

>dim colMyCollection as New Collection

>This instantiates the Collection Object.

begin 666 Mark J. Brown.vcf
M0D5'24XZ5D-!4D0-"E9%4E-)3TXZ,BXQ#0I..D)R;W=N.TUA<FL[2BX-"D9.






M;F4L($-!(#DR-C$X/3!$/3!!55-!#0I54DPZ#0I54DPZ:'1T<#HO+W=W=RYP
M;&%T<V]F="YC;VT-"D5-04E,.U!2148[24Y415).150Z;6)R;W=N0'!L871S

`
end
end


Sun, 26 Nov 2000 03:00:00 GMT  
 Adding Objects to Collections

That's what I am doing. When my base object is initialized I am setting my
dependant objects as new.

--

Mark J. Brown

Software Engineer
Platinum Software Corporation
http://www.platsoft.com

Quote:


>>You need to declare the collection As New
>>dim colMyCollection as New Collection
>>This instantiates the Collection Object.

>Actually, it doesn't.  The collection is instantiated the first time you
>reference it.  The DIM does not create the instance.  Instead, VB has to
>check every time you reference colMyCollection to see if it exists or not.
>It's generally a better idea to use:

>Dim colMyCollection As Collection
>Set colMyCollection=New Collection

>The advantage is that you control when the collection is created and
>destroyed and can test it with "If colMyCollection Is Nothing Then".  Using
>"Dim ... As New ..." gives up a lot of control over the objects.  It may
>also have a performance hit if VB does actually test the instance on every
>reference (instead of trapping the error if you reference it without
>creating it).

begin 666 Mark J. Brown.vcf
M0D5'24XZ5D-!4D0-"E9%4E-)3TXZ,BXQ#0I..D)R;W=N.TUA<FL[2BX-"D9.






M;F4L($-!(#DR-C$X/3!$/3!!55-!#0I54DPZ#0I54DPZ:'1T<#HO+W=W=RYP
M;&%T<V]F="YC;VT-"D5-04E,.U!2148[24Y415).150Z;6)R;W=N0'!L871S

`
end
end


Mon, 27 Nov 2000 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Adding objects to collections

2. VB5.0 Type mismatch when adding object to collection

3. ? Collection add object -> usercontrol add control

4. ? Collection add object -> usercontrol add control

5. Adding objects to a collection object

6. Adding to Collection Object

7. How do you add object to a collection

8. More on Adding a class object to collections

9. Adding user defined types to a collection object

10. Storing objects added to a collection in a database

11. Adding User-Defined Type to a Collection Object

12. Collection.Add and Collection.Item question?

 

 
Powered by phpBB® Forum Software