Object creation based on string

  • Thread starter Thread starter Phill
  • Start date Start date
P

Phill

I am storing the name of a Crystal Report in a combobox.
I want to create an instance of this report based on this
string. Is this possible? Or am I going to have to code
a Case statement? This is my current code:
crReportDocument = new rptReferredPatients ();

But I have rptReferredPatients stored in
cboReportsAvailable.ValueMember

Thanks.
 
Thanks Jay...I just don't understand your suggestion.
Here is my code snippet. The line I want to replace is
"this.OpenForm(new frmRptParmReferredPatients());"
frmRptParmReferredPatients is stored in
this.cboReportsAvailable.SelectedValue. How can this be
done?

//Fill the Reports Available combobox
string ConnStr =
System.Configuration.ConfigurationSettings.AppSettings
["ConnStr"];

this.cboReportsAvailable.DataSource= new ReportsCollection
(new SqlConnection(ConnStr));

cboReportsAvailable.DisplayMember = "ReportName";
cboReportsAvailable.ValueMember = "ReportDefinition";

this.OpenForm(new frmRptParmReferredPatients());
 
Phill said:
Thanks Jay...I just don't understand your suggestion.
Here is my code snippet. The line I want to replace is
"this.OpenForm(new frmRptParmReferredPatients());"
frmRptParmReferredPatients is stored in
this.cboReportsAvailable.SelectedValue. How can this be
done?

How about this...

Form toOpen =
Activator.CreateInstance(Type.GetType(this.cboReportsAvailable.SelectedValue
)) as Form;
this.OpenForm( toOpen );

Not totally sure if that's right, but could be a start!?

Tobin Harris
 
Phill,
The code that Tobin Harris gave should work.

If not the URL I gave has a number of examples of how to use
Activator.CreateInstance.

Hope this helps
Jay

Phill said:
Thanks Jay...I just don't understand your suggestion.
Here is my code snippet. The line I want to replace is
"this.OpenForm(new frmRptParmReferredPatients());"
frmRptParmReferredPatients is stored in
this.cboReportsAvailable.SelectedValue. How can this be
done?

//Fill the Reports Available combobox
string ConnStr =
System.Configuration.ConfigurationSettings.AppSettings
["ConnStr"];

this.cboReportsAvailable.DataSource= new ReportsCollection
(new SqlConnection(ConnStr));

cboReportsAvailable.DisplayMember = "ReportName";
cboReportsAvailable.ValueMember = "ReportDefinition";

this.OpenForm(new frmRptParmReferredPatients());


-----Original Message-----
Phill,
I normally use System.Type.GetType to get a type object based on the string.

Then I use System.Activator.CreateInstance to create an instance of an
object based on that type.

For details & info see:
http://www.yoda.arachsys.com/csharp/plugin.html

Hope this helps
Jay




.
 
Back
Top