-
Hi,
I have written a class module.
I want to make one of the functions return a user defined type that i have defined as:
Code:
Public Type FirstLastLevel
FirstLevel As Integer
LastLevel As Integer
End Type
I have made this declaration in a the general section of a normal module. My function is defined as:
Code:
Public Function GetLevelRange(LowerHeight As Long, Upperheight As Long) As FirstLastLevel
GetLevelRange.FirstLevel = 1
GetLevelRange.LastLevel = 4
End Function
When i try to run the program i get the following error message:
Compile error:
Only public user defined types defined 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
Does anyone know if you can return user defined types from a class module procedure - it looks as though you can but i must be doing something wrong. I tried moving the type declaration into the class module but that didn't help wither.
What do they mean by "public object modules" - is this a class module or a normal module - or a special kind of one of these.
Any help would be greatly appreciated,
Mark
-
Put both of your codes in a Standard Module. I have modified your GetLevelRange Function. You can see the changes made in Bold letters.
Code:
Public Type FirstLastLevel
FirstLevel As Integer
LastLevel As Integer
End Type
Public Function GetLevelRange(LowerHeight As Long, Upperheight As Long) As FirstLastLevel
GetLevelRange.FirstLevel = UpperHeight
GetLevelRange.LastLevel = LowerHeight
End Function
Now put the following code in a CommandButton to call it.
Code:
Private Sub Command1_Click()
' Declare RetVal as our UDT, otherwise,
' we'll get an Error
Dim Retval As FirstLastLevel
Retval = GetLevelRange(1, 2)
End Sub
-
Thanks Megatron, that will work however in my case i need the function itself to be part of the class module.
This is because the function needs to do some processing of the private data to the class module (a table with information for various levels which i need to loop through to determine these level numbers - anyway). So i think the function needs to be in the class module and as such i am not sure if it is then possible to use the UDT.
What i am really trying to do is avoid having to make it into 2 functions, one to return the upper level and one to return the lower level - because you can return both with the UDT - thats one of my faves - returning multiple values by using UDTs but i havn't done it with a class module before - only normal modules.
Thanks again for your reply though...
Mark
-
funkyd77, put your public type in a separate dll project within a public multiuse classmodule, and have fun ;)