• user defined types? (Office XP + SP1)

    Author
    Topic
    #370473

    Here’s the error message: [indent]


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


    [/indent]User defined type – i know what that is
    public object module – not sure. How is this different from a Module or a Class module?
    coercion – yeah, that’s what i guess i need
    Here’s some code i was playing with in a Plain Ol’ Module to try to figure this out:

    Option Explicit
    
    Public Type udt '   user defined type
        i As Integer
    End Type
        
    Dim x As udt
    Dim anx As Variant
    
    Public Function CreateX(i As Integer) As Variant
        x.i = i
        '   CreateX = CVar(x)   <<< doesnt work
        '   CreateX = x         <<< doesnt work
    End Function
    
    Public Sub test()
        '   anx = CreateX(42)   <<< doesnt work
        CreateX (42)
        anx = x
    End Sub
    

    A clue! A clue! A reference to a text will do!
    My kingdom for a clew, already!

    Viewing 2 reply threads
    Author
    Replies
    • #586347

      I realize this is a nonresponsive comment, but why would you ever want to “coerce” a UDT to a variant? I thought the point of a UDT was its highly structured nature (I like to use arrays of them now and again). A variant is something of a mystery. Although one can interrogate a variant for its “subtype,” what can you do if you learn that VarType(x) = vbUserDefinedType? How does it help?

      • #586349

        I didnt start out to coerce anything! That was the msg from vba, tho.

        Your questions are dead on – i’ve been asking them myself.

        What the problem revolves around is the Scripting.dictionary object, for which the documentation says “you can put any kind of data into it.” I think that the dox are confusing me with their use of “any”, since i view a UDT as “any”, whereas i am thinking now that the data type must be defined in a class (maybe they screwed up and wrote ‘object’ instead of ‘class’?).

        That is the hypothesis under which i shall go back and try again with the dictionary.

      • #586358

        My hypothesis is correct!
        If I define a Class that contains two data members, like this

        Option Explicit
        
        Public anInt As Integer
        Public aLong As Long

        then i can use it like this

        Option Explicit
            
        Dim x As Class1
        
        Public Sub PlayWithDictionaryObject2()
            Dim d As Scripting.Dictionary
            Set d = New Scripting.Dictionary
            
            Set x = New Class1
            d.Add "Athens", x    'Add some keys and items.
            d.Add "Belgrade", 0
            d.Add "Cairo", 0
        

        Of course, the dictionary with a bunch of different kinds of data attached to it is a beast that requires special handling, which i will avoid.

    • #586374

      Change

      anx = x

      to

      anx = x.i

    • #586454

      Yes, UDTs can’t be used like that you have to define a class. UDTs are good for simple things but can’t be used like like the built-in data types (although you’d think they should be). The solution is to define the same data as a class and then you can use it like any other data type.

      I do this a lot then use the class object inside of collections or as returns from functions. You’d define the class almost exactly like the UDT and just use it as a data type. Add a class to your project (like you add a Form or a new Module). Then type the data that makes up the new class (the new data type).

      public int1 as integer
      public str1 as string
      public str2 as string

      Now outside of this class module you can define a variable of this data type.

      dim myDat as new myClass

      myDat.int1 = 5
      myDat.str1 = “deb”
      myDat.str2 = “woody”

      This works the same as any other built-in Excel object (Range, Worksheet and 1000 others). Since I use collections a lot I use this to carry around complex data. You can’t store a UDT in a collection.

      Deb bow

      • #586480

        Thanks deb-

        what threw me is that the language REQUIRES class data to be GLOBAL. What i expected was that class data must to be protected, if not private.

        • #586494

          AAAHHHH, I see… You assumed the error messages made sense dizzy They rarely do grin

          That one threw me for quite a while too and I didn’t get why my UDT failed until I read something in a book now I almost always use classes.

          Deb bingo

    Viewing 2 reply threads
    Reply To: user defined types? (Office XP + SP1)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: