Message 1 of 6
Assigning a user-defined type to a variant - an idea for better XData
Not applicable
04-23-2002
03:15 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi all,
To set the tone: I'm very experienced in VB, reasonably experienced in VBA,
and a newbie to AutoCAD (2002).
I like the idea of XData, but don't like the idea of having to remember what
thing is in what element (eg woodgrain is in XData(7)). So I had the idea
of using the XDataType 1004 (Binary Data) to hold a variant that holds a
user-defined type that I can structure however I like! (Notwithstanding the
127-byte limit for Binary data types.)
I tried something like this (simplified for this example):
Option Explicit
Private Type LineXDataType
AutoGenerated as Boolean
WoodStyle as Integer
End Type
Sub Whatever()
Dim LineObj(10) as AcadLine
Dim pt1(0 to 2) as Double, pt2(0 to 2) as Double
Dim XDataType(0 To 1) As Integer, XData(0 To 1) As Variant, LineXData As
LineXDataType
pt1(0) = 0: pt1(1) = 0: pt1(2) = 0
pt2(0) = 3: pt2(1) = 5: pt2(2) = 0
Set LineObj(1) = ThisDrawing.ModelSpace.AddLine(pt1, pt2)
LineXData.AutoGenerated = False
LineXData.WoodStyle = 3
XDataType(0) = 1001: XData(0) = "MyProject"
XDataType(1) = 1004: XData(1) = LineXData
LineObj(1).SetXData XDataType, XData
End Sub
However the compiler highlighted the statement "XData(1) = LineXData" and
gave the following error message:
Compile error: Only user-defined types defined in public object modules
can be coerced to or from a variant or passed to late-bound functions
Now I know that previously variants could not hold a user-defined type, but
that is supposedly no longer the case. Placing the cursor on the keyword
"Variant" in VBA and pressing F1 gives the help topic "Variant Data Type",
the second paragraph of which states:
A Variant is a special data type that can contain any kind of data
except fixed-length String data. (Variant types now support user-defined
types.)
So why am I getting this error? Based on the context of the error message I
tried moving the Type declaration and the Dim statements for the XData
variables to a module and making them Public but it didn't make any
difference.
If anyone can shed some insight as to what is happening here would have my
sincere appreciation.
Thanks
Wayne Ivory
Wespine Industries Pty Ltd
To set the tone: I'm very experienced in VB, reasonably experienced in VBA,
and a newbie to AutoCAD (2002).
I like the idea of XData, but don't like the idea of having to remember what
thing is in what element (eg woodgrain is in XData(7)). So I had the idea
of using the XDataType 1004 (Binary Data) to hold a variant that holds a
user-defined type that I can structure however I like! (Notwithstanding the
127-byte limit for Binary data types.)
I tried something like this (simplified for this example):
Option Explicit
Private Type LineXDataType
AutoGenerated as Boolean
WoodStyle as Integer
End Type
Sub Whatever()
Dim LineObj(10) as AcadLine
Dim pt1(0 to 2) as Double, pt2(0 to 2) as Double
Dim XDataType(0 To 1) As Integer, XData(0 To 1) As Variant, LineXData As
LineXDataType
pt1(0) = 0: pt1(1) = 0: pt1(2) = 0
pt2(0) = 3: pt2(1) = 5: pt2(2) = 0
Set LineObj(1) = ThisDrawing.ModelSpace.AddLine(pt1, pt2)
LineXData.AutoGenerated = False
LineXData.WoodStyle = 3
XDataType(0) = 1001: XData(0) = "MyProject"
XDataType(1) = 1004: XData(1) = LineXData
LineObj(1).SetXData XDataType, XData
End Sub
However the compiler highlighted the statement "XData(1) = LineXData" and
gave the following error message:
Compile error: Only user-defined types defined in public object modules
can be coerced to or from a variant or passed to late-bound functions
Now I know that previously variants could not hold a user-defined type, but
that is supposedly no longer the case. Placing the cursor on the keyword
"Variant" in VBA and pressing F1 gives the help topic "Variant Data Type",
the second paragraph of which states:
A Variant is a special data type that can contain any kind of data
except fixed-length String data. (Variant types now support user-defined
types.)
So why am I getting this error? Based on the context of the error message I
tried moving the Type declaration and the Dim statements for the XData
variables to a module and making them Public but it didn't make any
difference.
If anyone can shed some insight as to what is happening here would have my
sincere appreciation.
Thanks
Wayne Ivory
Wespine Industries Pty Ltd