Create a class on the fly

I’ve created a base class report object. Then I created a subclass for each particular report type. I’d like to create a particular report object based on a passed parameter, such as;

sub runReport( classname as string)

dim reportObj as varient

'I don’t know what to place here

set reportObj = createClass( classname )

call reportObj.print()

end sub

Is there something like that in LotusScript?

Thanks in advance.

Jeff

Subject: Create a class on the fly - This example does it

'Import the following code as a new Lotusscript Test agent:

Option Public

Option Declare

%INCLUDE “LSCONST.LSS”

Private Const gc_ERROR_INVALID_CLASS$=1

Private Const gc_REPORT_CLASSNAME_PRINTER$ = “Printer”

Private Const gc_REPORT_CLASSNAME_MESSAGEBOXER$ = “MessageBoxer”

Private Const gc_REPORT_CLASSNAME_DOES_NOT_EXIST$ = “Dummy”

Class Report

Private m_OutputLine As String



Sub New()

	m_OutputLine$ = "Hello World"

End Sub



Private Property Get OutputLine As String

	OutputLine$ = m_OutputLine$	

End Property



Sub Print

	' Overridden base class to output the report

End Sub

End Class

Class ReportPrinter As Report

Sub Print

	' Print the given text string on the status line (client) / log (server)

	Print Me.OutputLine$

End Sub

End Class

Class ReportMessageBoxer As Report

Sub Print

	' Display the given text string in a popup message.

	Messagebox Me.OutputLine$

End Sub

End Class

Class ReportClassFactory

Public Function createClass(strClassName As String) As Report

	Select Case strClassName

	Case gc_REPORT_CLASSNAME_PRINTER$

		Set createClass = New ReportPrinter

	Case gc_REPORT_CLASSNAME_MESSAGEBOXER$

		Set createClass = New ReportMessageBoxer

	Case Else

		Error gc_ERROR_INVALID_CLASS$,Getthreadinfo(LSI_THREAD_CALLPROC%) & " - " & Getthreadinfo(LSI_THREAD_PROC%) & |: Cannot instantiate a "| & strClassName & |" class.|

	End Select

End Function

End Class

Class ReportRunner

Private m_factory As ReportClassFactory



Sub New

	Set m_factory = New ReportClassFactory

End Sub



Sub runReport( strClassname As String)	

	Dim report As Report		

	Set report = m_factory.createClass(strClassname$)	

	Call report.print

End Sub

End Class

Class ReportRunnerTestHarness

Sub Run

	Dim runner As New ReportRunner		

’ Use printer class to print “Hello World” to status bar

	runner.RunReport(gc_REPORT_CLASSNAME_PRINTER$)

’ Use messageboxer class to popup “Hello World” as a message

	runner.RunReport(gc_REPORT_CLASSNAME_MESSAGEBOXER$)

’ Throw error

	runner.RunReport(gc_REPORT_CLASSNAME_DOES_NOT_EXIST$)

	

End Sub

End Class

Sub Initialize

Dim test As New ReportRunnerTestHarness

test.run

End Sub

Subject: RE: Create a class on the fly - This example does it

Thank you very much! That’s a great example and just what I was looking for.

Jeff

Subject: Create a class on the fly

At first glance I would say that you need to use the TypeName() function and a Select Case clause to switch to the correct class, and using it’s Print() method.