Adding user type variable to dictionary

J

Jonathan Blitz

I have defined a user type and wish to add it to a Dictionary as the value
with a string as the key.
I get the following error:

Only public user defined types 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.

The online help says:

This error has the following cause and solution:
a.. You attempted to use a public user defined type as a parameter or
return type for a public procedure of a class module, or as a field of a
public user defined type. Only public user defined types that are defined in
a public object module can be used in this manner.
But I am in a public module and the type is defined as a public type.
So what am I doing wrong?


--
Jonathan Blitz
AnyKey Limited
Israel

"When things seem bad
Don't worry and shout
Just count up the times
Things have worked themselves out."
 
J

John Nurick

Hi Jonathan,

I can't really understand the help message, but there's an explanation
here that appears relevant (it's a long URL so watch out for line breaks
inserted by the newsgroup software):


http://groups.google.co.uk/groups?q...=off&selm=O7dhtE9hBHA.2500@tkmsftngp02&rnum=2

which appears to say there's no easy way of doing what you want. However
you can probably work round it by replacing your UDT with a class with
properties that correspond to the fields in the UDT.

Here's the code from class module I just tried out:
'------------------------
'clsTest
Option Compare Database
Option Explicit

Dim m_Name As String
Dim m_Number As Long

Property Let Name(S As String)
m_Name = S
End Property

Property Get Name() As String
Name = m_Name
End Property

Property Let Number(N As Long)
m_Number = N
End Property

Property Get Number() As Long
Number = m_Number
End Property
'------------------------------


And here's the code from a code module that shows it working:
'------------------------------
Option Explicit

Dim dicD As New Scripting.Dictionary

Sub AddToDic(T As clsTest, K As String)
dicD.Add K, T
End Sub

Sub TestCT()
Dim T As clsTest

Set T = New clsTest
T.Name = "This is my name"
T.Number = 1
AddToDic T, "One"

Set T = New clsTest
T.Name = "Another Name"
T.Number = 2
AddToDic T, "Two"

Debug.Print "One", dicD("One").Name
Debug.Print "Two", dicD("Two").Name

dicD.RemoveAll
Set dicD = Nothing
End Sub
'-------------------------------
 

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