Adding User-defined Types to a Collection?

HedgePig

Board Regular
Joined
Jun 27, 2002
Messages
146
Help!

Can one add a user-defined object to a Collections object?

I'm getting a compile error ("ByRef argument type mismatch") when I try the following :
fred.Add Item:=shinfo

I have previously declared as
Dim fred As New Collection
Dim shinfo as WSinfo

and WSinfo is a userdefined type (Global) defined outside the subroutine.

If I "Dim shinfo as Integer" (instead of my user-defined type), the program compiles without any problem. Any help as to what I'm doing wrong will be appreciated!

Yours baffled
HedgePig
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Do you get the following error when you try to add the type to the collection?

Only public user defined types defined in public object modules can be used as parameters or return types for public procedures of class modules or as fields of public user defined types
 
Upvote 0
User defined types are very useful in their own fashion, but come with some restrictions. Using an object of a class you define might give you more flexibility.

The foll. works just fine.

Create a class module named Class1. In it declare a single public entity:
Code:
Option Explicit

Public x As Integer
[Normally, I would use Property Get and Property Let procedures with a private variable, but for now let's use a public variable x in the class module.]

In a standard module:<pre>

Option Explicit

Sub testColl()
Dim x As Class1, myColl As Collection, y As Class1
Set x = New Class1
Set myColl = New Collection
x.x = 10
myColl.Add x, CStr(x.x)
Set y = myColl(1): MsgBox y.x
Set y = myColl(CStr(10)): MsgBox y.x
End Sub</pre>

_________________
Regards,

Tushar
www.tushar-mehta.com
This message was edited by tusharm on 2002-08-02 15:30
This message was edited by tusharm on 2002-08-02 15:56
This message was edited by tusharm on 2002-08-02 15:58
 
Upvote 0
Hello Mark and Tushar

Thanks for your help. Mark - No I didn't get that error message. But Tushar has solved the problem.

Tushar - I set up a class module with public members and it works perfectly. Thanks for the suggestion.

Regards
HedgePig
 
Upvote 0

Forum statistics

Threads
1,215,987
Messages
6,128,138
Members
449,426
Latest member
revK

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top