Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Collections and userdefined types

Status
Not open for further replies.

yamamoto

Programmer
Oct 6, 2003
3
0
0
GB
I have a problem with adding a record defined by the type statement to a collection. I have tried to define the collection and the record both public and private and in a module.

eg:
Private Type groupRecord
name As String
no As Integer
Index() As Integer
End Type
private group as groupRecord
Private groupCol As New Collection

private sub checkGroup()
.....
groupCol.add group
.....
end sub

But I get this error message:

<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>

I also want to know if it is possible to ship a userdefined type as an argument in some way or define a public userdefined variable which can be modified from anywhere in the program. I really need to have acess to the collection from at least two forms. Or if there are any good suggestions for solving such problems when the vb datatypes can't be used.

Please help me with this...
thank you
 
I have also come across this problem a long time ago. I solved it by defining the UDT as a class instead (therefore each element (name, no etc) would be a property, then you will be able to add each instance into the collection.

Madlarry

 
madlarry is correct - you cannot add a UDT to a collection, but you can add an instance of a class.

Code:
Private Type groupRecord
   name As String
   no As Integer
   Index() As Integer
End Type

Private objgroupRecord as groupRecord

Public Property Get name as String
  name = objgroupRecord.name
End Property

Public Property Let name(name as String)
  objgroupRecord.name = name
End Property

... etc.

You can also create your own Collection object which explicitly handles instances of this class only, which has some nice side-effects, like parameter completion working, etc.

Search for Creating your own Collection Classes in MSDN (try looking in VB Programmers manual under Programming with Objects)

Build a house of Bricks if you can - as the piggies found it, its worth it in the long run ;-)

Chaz
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top