Create object from its name (string)

  • Thread starter Thread starter tekanet
  • Start date Start date
T

tekanet

Hello folks!

Please don't try to crucify me.. I wonder if it's possible to create
an object by its name, passed in a string. Example:

I have some reports and a wrapper class that shows them. This way:

Dim rptDummy As New Report
Dim sReport As String

sReport = Request.QueryString("REPORT_NAME")

Select Case sReport
Case "rptReportOne"
Set rptDummy = rptReportOne
Case "rptReportTwo"
Set rptDummy = rptReportOne

And so on..

I'd like to do something like this:

Dim rptDummy As New Report
Dim sReport As String

Set rptDummy = SOMETHING(sReport)

Any ideas? TIA, tK
 
It is quite possible, using reflection. But what exactly is rptReportOne and
rptReportTwo? Are they types? Or class properties? Or what exactly?
 
Jerry Pisk said:
It is quite possible, using reflection. But what exactly is rptReportOne and
rptReportTwo? Are they types? Or class properties? Or what exactly?

Sorry Jerry for not explaining this before. They are classes. More
precisely, they are Active Reports (from DataDynamics), but I think we
can talk about it generally, considering classes.

I'll take a look to the namespaces you and Richard purposes.

tK
 
I'm assuming classes means types. Here's what you do:

Type reportType = Type.GetType(reportName);
ConstructorInfo reportConstructor =
reportType.GetConstructor(Type.EmptyTypes);

report = (Report)reportConstructor.Invoke(null);

If the constructors take parameters you need to add those.

Jerry
 
Back
Top