Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Object Reference in VB
Message
From
02/04/2001 13:58:44
Shane Gilbert
Oklahoma State Department of Education
Norman, Oklahoma, United States
 
 
To
02/04/2001 13:28:11
General information
Forum:
Visual FoxPro
Category:
Other
Miscellaneous
Thread ID:
00490916
Message ID:
00491006
Views:
12
>>>>I need to figure out how to get an object reference from a string name in Visual Basic. I have a function that takes a string parameter, which is the name of an object. I want to be able to reference that object and call one of it's methods. Something like the Eval() function. Any suggestions?
>>>
>>>I don't believe that's possible in VB.
>>>Why not just pass in the object?
>>
>>Here is what I am trying to do. I am trying to create a COM object that will use the Data Reports tool to generate HTML pages that can be printed. I will call this from ASP pages with some parameters to grab the data I need and then run the report out to an HTML page. I have it all working, but I would like to be able to simply create the new report and add it to the project without have to code in a SELECT CASE for every report I add. It seems like there should be a way to do this, even Java has a ClassForName method to do this kind of thing.
>
>What would the class string passed in look like?
>Is it in the ASP page that you'd like to call the specific report class?
>
>You can use a CREATEOBJECT() on a string to create what kind of class you would like.
>Also, you can Implement an interface for it, e.g.
>
'-------------
>' Create a report interface class, e.g. IReport
>' function must be empty - just the function interface
>Public Function RunReport(Optional ByVal param1 As Variant = "", _
>                       Optional ByVal param2 As Variant = "", ....)
>End Function
>
>'-------------
>' Create specific report class(es)
>' e.g. MonthlyReport
>' Declarations
>Option Explicit
>Implements IReport
>
>' this function will have to be coded
>' since it is an Implemented function
>Public Function IReport_RunReport(Optional ByVal param1 As Variant = "", _
>                            Optional ByVal param2 As Variant = "", ....)
>
>   '-------------
>   ' code to run the specific report here
>   '-------------
>
>End Function
>
>'-------------
>' in your COM object called from the ASP page
>' add a reference to the project to the interface class
>' or add the cls itself to the project
>
>' e.g. in the ASP page, the report factory is called, which then calls
>' the specific type of report - e.g.
>' ReportFactory.RunReportFromASP("MyReports.MonthlyReport", #2001/01#)
>
>Public Function RunReportFromASP(cClassName As String, _
>                            Optional ByVal param1 As Variant = "", _
>                            Optional ByVal param2 As Variant = "", ....)
>
>   Dim oReport As IReport
>
>   Set oReport = CreateObject(cClassName)
>
>   oReport.RunReport(param1, param2, ....)
>
>End Function
>When the MonthlyReports class (or any other class implementing the IReport interface) is compiled, if the IReport_RunReport() function is empty, an error will be thrown. This way, you ensure that the RunReport() function will be available for the object created inside your COM server, since the object created will be assumed to implement the IReport interface.

Thanks for the information. This almost gets me where I want to be. Let me explain in greater detail what I am trying to do. I have an Active-X DLL project that contains a class (aspHTMLreport), a dataReport object, and a dataEnvironment object. I have the following method in my class:
Public Function printReport(reportName As String) As String
    Dim strFileName As String, oDataReport As DataReport
    strFileName = CreateTempFile("rpt")
    'need some way to set oDataReport to reportName object
    Set oDataReport = rptHTML
    
    oDataReport.ExportReport rptKeyHTML, strFileName, True, False, rptRangeAllPages
        
    While oDataReport.AsyncCount > 0
         DoEvents
    Wend
    Unload oDataReport
    
    Dim outStr As String, F As Integer
    F = FreeFile                       'Get a file handle
    Open strFileName For Input As F    'Open the file
    outStr = Input$(LOF(F), F)         'Read entire file into text box
    Close F                            'Close the file.
    
    Kill strFileName
    
    printReport = outStr
End Function
rptHTML is the DataReport object that is part of the project. I want to be able to add several reports and call them with this method without having to add any code to the class itself, just add the DataReport and the DataEnvironment to the project. Does this make sense or is there a better way?
--Shane
Previous
Reply
Map
View

Click here to load this message in the networking platform