Unable to add user-defined data types to a collection

A

Adrian

Hi,

I am adding a user-defined data type to a collection as illustrated below
but failed ... Can somone please help? Thanks..


'All codes here are written in a module "ErrSeeder"
'--------------------------------------
(General) (Declarations)
Type Seed
name as string
no as integer
End Type
'--------------------------------------
Sub test()

Dim c As New Collection
Dim s As Seed
s.Name = "Watermelon Seed"
s.no = 1

c..Add s <- Compliation fail at this point with the following error :
Only user-defined types defined in public object modules can be coerced to
or from a variant or passed to late-bound functions

End Sub
'--------------------------------------
May I know where can I define my user data type so that the code above will
work ? What does it mean by public object modules ?

Thanks.

Regards,
Adrian
 
P

Peter Beach

Hi Adrian,

You can only add objects to collections, not user defined types :-(

Create a small class object and add that to the collection and you should be
fine.

HTH

Peter Beach
 
J

Jamie Collins

Peter Beach said:
You can only add objects to collections

That's not correct:

Sub test2()
Dim c As New Collection
Dim s As String
Set c = New Collection
s = "Text"
c.Add s
End Sub

Jamie.

--
 
P

Peter Beach

Hi James,

You are correct. You can also add simple data types to a collection. What
you can't add to a collection is a user-defined type.

Regards,

Peter Beach
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top