dmars
New Member
- Joined
- Nov 19, 2013
- Messages
- 17
This is my first attempt to make an LIFO stack in Excel VBA, and the problem I'm running into is that Excel doesn't like my user defined type as a parameter I pass to a function. I get the compiler error:
"Only public user defined types defined in public object modules can be used as parameters or return type for public procedures of class modules or as fields of public user defined types",
with an instance of my user defined type highlighted.
My type definition is public and so are all my subs, so I don't see what Excel is complaining about. But I don't really understand what this compile error means.
(Please note that I've only sketched out what my pop and push functions will look like, and I lifted them out of sample code so they probably need some fine tuning to work in this context. I haven't done that or tried to run the code because I can't get past this compile error! I only left them in here to give a more complete idea of where I'm going with all this. What I really want to do first is resolve the compiler error, and hopefully understand why I'm getting it.)
Here's my code, and the cs in "count = Push(pStack, cs)" is highlighted when I get the compiler error (it didn't complain about any of the previous uses of cs):
Thanks in advance for any help with this
"Only public user defined types defined in public object modules can be used as parameters or return type for public procedures of class modules or as fields of public user defined types",
with an instance of my user defined type highlighted.
My type definition is public and so are all my subs, so I don't see what Excel is complaining about. But I don't really understand what this compile error means.
(Please note that I've only sketched out what my pop and push functions will look like, and I lifted them out of sample code so they probably need some fine tuning to work in this context. I haven't done that or tried to run the code because I can't get past this compile error! I only left them in here to give a more complete idea of where I'm going with all this. What I really want to do first is resolve the compiler error, and hopefully understand why I'm getting it.)
Here's my code, and the cs in "count = Push(pStack, cs)" is highlighted when I get the compiler error (it didn't complain about any of the previous uses of cs):
Code:
Option Explicit
Public Type element
parent As Long
child As Long
level As String
End Type
Public Function init(newe As element)
newe.parent = 0
newe.child = 0
newe.level = 0
init = 1
End Function
Public Function Pop() As Variant
With pStack
If .count > 0 Then
Pop = .Item(.count)
.Remove .count
End If
End With
End Function
Public Function Push(Stack, newElement As Variant) As Variant
With Stack
.Add newElement
Push = .Item(.count)
End With
End Function
Public Sub test()
'pStack is a collection of parent/child elements, to be used as a LIFO stack
Dim i As Long, j As Long, ret As Integer, pStack As Collection, count As Long
'create LIFO stack
Set pStack = New Collection
'init variables
i = 1
j = 101
For i = 1 To 10
Dim cs As element
ret = init(cs)
With cs
'For i = 1 To 10
'assign quasi-random values to look at in stack
cs.parent = i
cs.child = 2 * j
count = Push(pStack, cs)
End With
i = i + 1
Next
End Sub
Thanks in advance for any help with this