ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   compiler error: public object module (https://www.excelbanter.com/excel-programming/373589-compiler-error-public-object-module.html)

Wang

compiler error: public object module
 
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

moon[_7_]

compiler error: public object module
 

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








"Wang" schreef in bericht
...
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




Wang

compiler error: public object module
 
Dear Moon,
I've changed the program according to your reply. It works fine! Thank you
very much!
Wang

"moon" wrote:


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








"Wang" schreef in bericht
...
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






All times are GMT +1. The time now is 10:09 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com