CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2003
    Posts
    322

    Error message: "Only user defined types define din public modules can be coerced to/f

    the full message is as follows

    "Only user defined types defined in public modules can be coerced to/from a variant or passed to late-bound functions."

    I've defined a UDT in a class.

    Private Type dblParam
    Val As Double
    name As String
    desc As String
    End Type


    and instantiated it as such

    Private mDbl As dblParam
    dim dictFormatParams as New Dictionary

    I have many of these, and want to add them to a containter object, a dictionary in this case.

    During compile time, this line causes the error displayed at the top of this post

    mDbl.name="Frequency"
    mDbl.val = 1.0E-6

    dictFormatParams.Add mDbl.name, mDbl <- dictionary- causes compile error messge (see top of this post)


    I don't understand why VB can't deal with UDT's defined in a class
    I don't want to move these variable instaniations outside of the class, it defeats the whole purpose of using a class.
    Last edited by cappy2112; January 31st, 2006 at 08:20 PM.

  2. #2
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    516

    Re: Error message: "Only user defined types define din public modules can be coerced

    Couple of options here. First (my personal preference) would be to get rid of the UDT passing and create a class describing the UDT and passing that back and forth instead. Second, if you must, you can do something like this: Make the type definition private in both the calling module and the class, and then pass it as a null delimited string. You'll need a function in the class to build the string and a function in the module to rebuild it into the UDT. I.e.
    Code:
    'Outside the class.
    Private Function LoadType(sReturn As String) As dblParam
    
        Dim sTemp() As String, uOut As dblParam
        
        sTemp = Split(sReturn, Chr$(0))
        uOut.Val = CDbl(sTemp(0))
        uOut.name = sTemp(1)
        uOut.desc = sTemp(2)
        
        LoadType = uOut
        
    End Function
    
    'Inside the class.
    Public Function ReturnsUDT() As String
    
        Dim sOut As String, uTemp As dblParam
        
        uTemp = Whatever
        sOut = CStr(uTemp.Val) & Chr$(0) & uTemp.name & Chr$(0) & uTemp.desc
        
        ReturnsUDT = sOut
    
    End Function
    
    'Call it like this:
    uVariable = LoadType(Class.ReturnsUDT)

  3. #3
    Join Date
    Apr 2003
    Posts
    322

    Re: Error message: "Only user defined types define din public modules can be coerced

    Quote Originally Posted by Comintern
    Couple of options here. First (my personal preference) would be to get rid of the UDT passing and create a class describing the

    I see the light now.

    Thanks!

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

Featured