User defined type question

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):

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
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Your Type definition is not in a Public Object module. Unfortunately, in VBA you can't declare a Public UDT in an Object module. I suggest you create an Element class module and use that instead of a UDT, or make the type private and encapsulate it all in a class.

Depending on the rest of the code, you may also be able to simply change your argument types to 'element' rather than Variant.
 
Last edited:
Upvote 0
Thanks, I'll try that. This is my first foray into this type of thing, so I'm sure I have lots to learn.
 
Upvote 0

Forum statistics

Threads
1,223,248
Messages
6,171,011
Members
452,374
Latest member
keccles

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top