Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using Variable (Dynamic) Class Name

Status
Not open for further replies.

tonylmiller

Programmer
Apr 18, 2002
16
US
Normally one would write:

Class myClass = new Class();

But I want "Class" to change. Can I make the name of the class a variable? How?

I used

Object o = Activator.CreateInstance("myAssembly","namespace" + myType);

but the object "o" has the type {System.Runtime.Remoting.ObjectHandle} with the wrapped object being myType. So I cannot access o's properties and methods. How do I make o of the type myType so that it can access myType's properties and methods?

Specifically what I want to do is instantiate an instance of a report, like this:

DefinedReport myReport = new DefinedReport();

But I want to pass in a variable for DefinedReport, instead of it being predefined.

Then I want to set its data souce and use it as the source for a report viewer.

myReport.SetDataSource(dataSet);
myReportViewer.ReportSource = myReport;

Any help would be greatly appreciated.

Tony
 
You've already explored your two choices in this matter:
If you know the name of the class, you can do strong typing so you get all your intellisense

If you don't know the name of the class at design-time, you can go through the CreateInstance route, but you don't get strong typing (it's an Object) and you don't get your intellisense.

One thing to try is to define an interface that your target classes implement. You'd still go through the CreateInstance to instantiate them, but you'd get intellisense (through the interface definition).

Chip H.
 
Thank you for your reply. My problem is that the methods and properties in the report class are not available. The statement:

myReport.SetDataSource(dataSet);

gives an error because myReport does not contain that method (because it is not of the correct type, I suppose).
 
thats what he meant by using an interface.

Define a base report like:

public interface IReport
{

void SetDataSource(DataSet ds);

}


Make all of the report classes derive from this interface, ie:

public class MyReport : IReport
{
public void SetDataSource(DataSet ds)
{
// insert code here
}
}


Then at design time, instead of:

object o = Activator.CreateInstance ("myAssembly","namespace" + myType);

do:

IReport myReport = Activator.CreateInstance ("myAssembly","namespace" + myType);

Then myReport.SetDataSource, and any other function you provide a valid signature for in the interface will be callable.
 
Wow, thanks a bunch!

I put the "public interface IReport" code into my "Reports" class. When I try to build

IReport myReport = Activator.CreateInstance ("myAssembly","namespace" + myType);

I get the error:

The type or namespace name 'IReport' could not be found (are you missing a using directive or an assembly reference?)

So, when I type "Reports.", the intellisense shows "IReport" as one of the options, so I know that it sees IReport. But if I build with

Reports.IReport myReport = Activator.CreateInstance ("myAssembly","namespace" + myType);

I get the error:

The type or namespace name 'IReport' does not exist in the class or namespace 'Reports' (are you missing an assembly reference?)

Is there possibly something wrong with my syntax or something?
 
Sounds like a namespace problem - make sure they match in your sourcefiles.

Another thing (possibly after you fix that problem) is you might have to cast the return type to IReport to shut the compiler warnings up:
Code:
IReport myReport = (IReport)Activator.CreateInstance ("myAssembly","namespace" + myType);
Chip H.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top