User defined type problem

R

R Avery

I have a public User defined type that i would like to use throughout my
code. I have put it in a module.


However, when I have a class that contains a scripting.dictionary of
these types return the Type in a function, I get the following error:

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


Then, when I try to put the public type definition in the class module,
it complains:

"Cannot define a Public user-defined type within an object module"


What is going on? How can I get around this? It seems as though the
errors are saying two contradictory things. Any help would be most
appreciated.
 
B

Bob Phillips

Not sure what you exactly di, but my test worked okay. This is what I did

I created a public type in a standard code module, with a long and a string
element.

I declared a variable of that type in a class module, and had 2 methods,.
one to set the type variable, one to return the value

I initiated an object of that class, and ran the 2 methods.

Everything worked as I expected.

Here is the code.

============================
Standard code module
============================
Option Explicit

Public Type myType
myLong As Long
myString As String
End Type

Sub test()
Dim newClass As Class1

Set newClass = New Class1
newClass.SetValues 17234, "Bob"
newClass.ReturnValues
Set newClass = Nothing
End Sub

============================
Class module
============================
Option Explicit

Dim typeTest As myType

Public Sub SetValues(val1, val2)
typeTest.myLong = val1
typeTest.myString = val2
End Sub

Public Sub ReturnValues()
Debug.Print "Emp #" & typeTest.myLong & " is " & typeTest.myString
End Sub



--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 
R

R Avery

I'm afraid I didn't say exactly what i meant. To clear up all
confusion, the code is below.

The problem occurs when I want to store many of these Types in a
Collection or Dictionary. The compiler complains about various things,
depending on where and what scope the Type is defined with.

Basically, I want to store a LOT of MyType records in a dictionary so
that I can immediately summon on by Key. If I cannot do this directly,
I guess an alternative would be to store them in an array, and store the
keys and array indices in a dictionary. However, this is a little less
intuitive and natural, and a lot more annoying. Any help would be
appreciated.


################################################
Module code
################################################


Type MyType
Item1 As String * 6
Item2 As Double
End Type

################################################
Class Module code
################################################





Private colItems As Collection


Private Sub Class_Initialize()
Set colItems = New Collection
End Sub

Public Sub Add(ByRef WhichItem As MyType)
colItems.Add WhichItem
End Sub

Public Function Item(ByVal Index As Long) As MyType
Item = colItems.Item(Index)
End Function
 

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