Results 1 to 4 of 4

Thread: User Defined Types in Class Modules

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2000
    Location
    Sydney, Australia
    Posts
    196

    Angry

    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

  2. #2
    Guest
    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

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2000
    Location
    Sydney, Australia
    Posts
    196
    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

  4. #4
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    funkyd77, put your public type in a separate dll project within a public multiuse classmodule, and have fun
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width