compiler error: public object module


G

Guest

Dear gurus:
Upon compiling the following code I receive the error message:
"Compiler error
Only user-defined types defined in public object modules can be coerced to
or from a variant or passed to late-bound functions."

Option Explicit
Type MyType
x As Integer
y As Integer
End Type
Public Sub MyTest()
Dim Coord(10) As MyType
Call FillCoord(Coord)
Call PrintCoord(Coord)
End Sub
Private Sub FillCoord(Coordinate As Variant)
Dim i As Integer
For i = 1 To 10
Coordinate(i).x = i + 1
Coordinate(i).y = i + 3
Next i
End Sub
Public Sub PrintCoord(Coordinate As Variant)
Dim i As Integer
For i = 1 To 10
Debug.Print Coordinate(i).x,
Debug.Print Coordinate(i).y
Next i
End Sub

This piece of code was in Module1 under Modules. In order to avoid the
compiler error, I removed the type definition to another module Module2. Now
under Modules there are Module1 and Module2. Module1 contains the following
code:

Option Explicit
Public Sub MyTest()
Dim Coord(10) As MyType
Call FillCoord(Coord)
Call PrintCoord(Coord)
End Sub
Private Sub FillCoord(Coordinate As Variant)
Dim i As Integer
For i = 1 To 10
Coordinate(i).x = i + 1
Coordinate(i).y = i + 3
Next i
End Sub
Public Sub PrintCoord(Coordinate As Variant)
Dim i As Integer
For i = 1 To 10
Debug.Print Coordinate(i).x,
Debug.Print Coordinate(i).y
Next i
End Sub

And Module2 contains the following code:

Option Explicit
Type MyType
x As Integer
y As Integer
End Type

But the compiler error remains the same. How should I change the
organization of the code to avoid this compiler error? Many thanks in advance!

Wang
 
Ad

Advertisements

M

moon

You can put it all in one single module:


Private Type MyType
x As Integer
y As Integer
End Type

Public Sub Test()
Dim Coord(10) As MyType
Call FillCoord(Coord())
Call PrintCoord(Coord())
End Sub


Public Sub FillCoord(ByRef Coordinate() As MyType)
Dim i As Integer
For i = 0 To 10
Coordinate(i).x = i + 1
Coordinate(i).y = i + 3
Next i
End Sub

Public Sub PrintCoord(ByRef Coordinate() As MyType)
Dim i As Integer
For i = 0 To 10
Debug.Print Coordinate(i).x
Debug.Print Coordinate(i).y
Next i
End Sub
 
Ad

Advertisements

G

Guest

Dear Moon,
I've changed the program according to your reply. It works fine! Thank you
very much!
Wang
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top