Results 1 to 9 of 9
  1. #1
    4 Star Lounger
    Join Date
    Dec 2000
    Location
    Faifax, Virginia, USA
    Posts
    542
    Thanks
    0
    Thanked 0 Times in 0 Posts

    user defined types? (Office XP + SP1)

    Here's the error message:
    <hr>Only user defined types
    defined in public object modules
    can be coerced to or from a variant
    or passed to late-bound functions
    <hr>
    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:
    <pre>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
    </pre>


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

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: user defined types? (Office XP + SP1)

    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?

  3. #3
    4 Star Lounger
    Join Date
    Dec 2000
    Location
    Faifax, Virginia, USA
    Posts
    542
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: user defined types? (Office XP + SP1)

    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.

  4. #4
    4 Star Lounger
    Join Date
    Dec 2000
    Location
    Faifax, Virginia, USA
    Posts
    542
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: user defined types? (Office XP + SP1)

    My hypothesis is correct!
    If I define a Class that contains two data members, like this <pre>Option Explicit

    Public anInt As Integer
    Public aLong As Long</pre>

    then i can use it like this <pre>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
    </pre>

    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.

  5. #5
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: user defined types? (Office XP + SP1)

    Change

    anx = x

    to

    anx = x.i

  6. #6
    4 Star Lounger
    Join Date
    Dec 2000
    Location
    Faifax, Virginia, USA
    Posts
    542
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: user defined types? (Office XP + SP1)

    Thanks Howard - i think i understand it!

  7. #7
    5 Star Lounger jujuraf's Avatar
    Join Date
    Jun 2001
    Location
    San Jose, California, USA
    Posts
    1,061
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: user defined types? (Office XP + SP1)

    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 <img src=/S/bow.gif border=0 alt=bow width=15 height=15>

  8. #8
    4 Star Lounger
    Join Date
    Dec 2000
    Location
    Faifax, Virginia, USA
    Posts
    542
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: user defined types? (Office XP + SP1)

    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.

  9. #9
    5 Star Lounger jujuraf's Avatar
    Join Date
    Jun 2001
    Location
    San Jose, California, USA
    Posts
    1,061
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: user defined types? (Office XP + SP1)

    AAAHHHH, I see... You assumed the error messages made sense <img src=/S/dizzy.gif border=0 alt=dizzy width=15 height=15> They rarely do <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

    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 <img src=/S/bingo.gif border=0 alt=bingo width=15 height=22>

Posting Permissions

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