VBA array woes

X

xargon

Hi everyone,

I have a problem passing a class module instance to another class. I have
a class module as such:

Option Explicit

Private Items() As AquaInput
Private NumItems As Integer

' Add an item to the end of the array.
Public Sub Add(ByVal new_value As AquaInput)
' Add the new item.
NumItems = NumItems + 1
Items(NumItems) = new_value
End Sub

However, whenever I call this Add method from outside. it throws an error
that the Object does not support this property or method. However, it is
right there. The funny thing is that if I replace AquaInput with built in
type like String, it works fine!

What am I doing wrong here? It is killing me!

Thanks,
xargon
 
C

Chris Roth [ Visio MVP ]

I think you just need to call Redim on the array. You are adding an item off
the end of your array, as far as I can see.

I usually fake a typed collection using VB's Collection object. VBs
class-builder helps you do this too, I've just discovered (insert
embarrassed face here)

Class AquaInput
Public Name as String
...
End Class


Class AquaInputs

Private m_items as Collection

Public Sub Add( ai as AquaInput)
if (m_items is Nothing) then set m_items = New Collection
m_items.Add(ai)
End Sub

End Class

--

Hope this helps,

Chris Roth
Visio MVP
 
X

xargon

Hi Chris,

Thanks for replying. I do call Redim on the array. I took out that line
for sake of brevity. What I do not understand is why it should work for
strings and not for user defined types :((

Cheers!
xargon
 
C

Chris Roth [ Visio MVP ]

Yeah, I found that weird that the strings work. Primitive data types are
always handled differently than objects, but I'm too lazy to investigate
today...

--

Hope this helps,

Chris Roth
Visio MVP
 

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