M
MikeB
Right, I'm playing around with VBA.
I want to create a class with some methods. The class will manage
producing formatted output. I will call the class several times with
fragments of an output line and a tab position for each fragment. Then
I will call the class to write all the accumulated
fragments in a single output operation.
In my Class module I have a user-defined type of linePart that has a
tabPosition and a stringFragment.
I also have a collection object OutLIne, that is a collection of
lineParts.
In my code I'm trying to create a new linePart, and then add it to the
Collection object outLine.
I get a VB error at run-time that says: Only public user defined types
defined in public object modules can be used as parameters or return
types for public procedures of class modules or as fields of public
user defined types
I tried making the user defined type Public, but that simply created a
different error.
Where am I going wrong? Thanks.
This is the calling code: PrintTest
Option Compare Database
Option Explicit
Sub x1()
Dim pr As New PrMgr
pr.setPr "Append", "TestFile.txt"
pr.Say
pr.add 1, "Hello world"
End Sub
This is my Class module: prMgr
Option Compare Database
Option Explicit
Private boolReady As Boolean
Private Type linePart
tabPos As Integer
str As String
End Type
Private outLine As Collection
Private Sub Class_Initialize()
Dim num As Integer
Set outLine = New Collection
Debug.Print "PrMgr: Initialize" & Now()
'For num = 1 To outLine.Count()
' outLine.Remove 1
' Next
End Sub
Public Sub setPr(ByVal method As String, _
ByVal fn As String)
' this is here because I couldn't find a way to initialize
' the class with parameters to the constructor.
boolReady = False
If Not fn <> "" Then
fn = "Sample_output.txt"
End If
Select Case method
Case "Append"
Open fn For Append As 1
Debug.Print "Output opened for Append."
Case "Output"
Open fn For Output As 1
Debug.Print "Output opened."
Case Else
err.Raise vbObjectError + 1, "PrMgr.Set", "Invalid Printer
options"
End Select
End Sub
Public Sub add(ByVal mTabPos As Integer, _
ByVal mStr As String)
Dim item As linePart
'item = AddLineItem(tabPos, str)
item.tabPos = mTabPos
item.str = mStr
outLine.add item
Debug.Print "There are now " & outLine.Count & " items in outline."
End Sub
Private Sub Class_Terminate()
Close 1
Set outLine = Nothing
Debug.Print "PrMgr: Terminate"
End Sub
Sub Say()
Debug.Print "Hello World"
End Sub
I want to create a class with some methods. The class will manage
producing formatted output. I will call the class several times with
fragments of an output line and a tab position for each fragment. Then
I will call the class to write all the accumulated
fragments in a single output operation.
In my Class module I have a user-defined type of linePart that has a
tabPosition and a stringFragment.
I also have a collection object OutLIne, that is a collection of
lineParts.
In my code I'm trying to create a new linePart, and then add it to the
Collection object outLine.
I get a VB error at run-time that says: Only public user defined types
defined in public object modules can be used as parameters or return
types for public procedures of class modules or as fields of public
user defined types
I tried making the user defined type Public, but that simply created a
different error.
Where am I going wrong? Thanks.
This is the calling code: PrintTest
Option Compare Database
Option Explicit
Sub x1()
Dim pr As New PrMgr
pr.setPr "Append", "TestFile.txt"
pr.Say
pr.add 1, "Hello world"
End Sub
This is my Class module: prMgr
Option Compare Database
Option Explicit
Private boolReady As Boolean
Private Type linePart
tabPos As Integer
str As String
End Type
Private outLine As Collection
Private Sub Class_Initialize()
Dim num As Integer
Set outLine = New Collection
Debug.Print "PrMgr: Initialize" & Now()
'For num = 1 To outLine.Count()
' outLine.Remove 1
' Next
End Sub
Public Sub setPr(ByVal method As String, _
ByVal fn As String)
' this is here because I couldn't find a way to initialize
' the class with parameters to the constructor.
boolReady = False
If Not fn <> "" Then
fn = "Sample_output.txt"
End If
Select Case method
Case "Append"
Open fn For Append As 1
Debug.Print "Output opened for Append."
Case "Output"
Open fn For Output As 1
Debug.Print "Output opened."
Case Else
err.Raise vbObjectError + 1, "PrMgr.Set", "Invalid Printer
options"
End Select
End Sub
Public Sub add(ByVal mTabPos As Integer, _
ByVal mStr As String)
Dim item As linePart
'item = AddLineItem(tabPos, str)
item.tabPos = mTabPos
item.str = mStr
outLine.add item
Debug.Print "There are now " & outLine.Count & " items in outline."
End Sub
Private Sub Class_Terminate()
Close 1
Set outLine = Nothing
Debug.Print "PrMgr: Terminate"
End Sub
Sub Say()
Debug.Print "Hello World"
End Sub