How use a collection in a custom type?

M

mscertified

I'm assuming this is possible...

My type definition is:

Public Type typRptParms
strInc As String
colCap As Collection
End Type

I am trying to execute:

Dim myParms As typRptParms
Set myParms.colCap = New Collection
myParms.strInc = "110101111"
myParms.colCap.Item(9) = "Opened from" <== 'object required' error
myParms.colCap.Item(10) = "Opened to"

Can anyone see the problem?
 
G

George Nicholson

myParms.colCap.Item(9) = "Opened from" <== 'object required' error
Can anyone see the problem?

Yup. You need to brush up on the use of the .Add method of the Collection
object.

myparams.colCap.Add Item:="Opened from ", Key:= "9"
or
myparams.colCap.Add "Opened from ","9"

to retrieve a value:
somestring = myParams.colCap.Item("9")

Note that:
somestring = myParams.colCap.Item(9)
is *not* the same thing. That would retrieve the 9th item in the collection
(if there is one).
If you use a number as the Item argument, the item from that position within
the collection will be returned. If you use a string argument, the item
*with that specific key designation* will be returned.
As written, .Item("9") and .Item(1) would both point to the same member of
the collection.

Alternatively, you could:

For i = 1 to 10
' Add 10 empty strings to collection and give them keys "1" to "10".
myparams.ColCap.Add "",cstr(i)
Next i

If you did that, then:
myParms.colCap.Item(9) = "Opened from"
or
myParms.colCap.Item("9") = "Opened from"

would both change the same pre-existing member of the collection.


Note: I have a vague recollection that you can't use a custom collection as
a data type within a user-defined type, but it is a vague memory and details
are fuzzy, so I'm not sure. I do seem to recall having to create a custom
Class (where a custom collection isn't a problem), rather than a Type, as a
workaround. In any case, I may have been trying to do something entirely
different. If that were the problem you would be getting a "...user defined
type not allowed..." error, so you don't need to panic unless that pops up,
but I thought it worth at least a heads-up.
 
M

Marshall Barton

mscertified said:
I'm assuming this is possible...

My type definition is:

Public Type typRptParms
strInc As String
colCap As Collection
End Type

I am trying to execute:

Dim myParms As typRptParms
Set myParms.colCap = New Collection
myParms.strInc = "110101111"
myParms.colCap.Item(9) = "Opened from" <== 'object required' error
myParms.colCap.Item(10) = "Opened to"


I haven't tried using a collection as a member of a Type,
but you have to use the Add method to put an item in a
collection:
myParms.colCap.Add "Opened from", "key string"
Note that the key string should not be a number, because a
reference to an item indexed by a number will return the
item at the position indicated by the number (or an error if
there is no item at that position).

Are you sure you can't use an array instead of a collection?
 

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