Using reflection to instantiate a class

G

Guest

Hello,
I have a problem where by wish to instantiate an object say NewReport
to a
specific class, all inherited from the base class MyReport, I have the
classname as a string and I don't want to use a massive Case Statement, I
wish to use Reflection as this is more efficient. This is how I would do it
in a case statement, how can I do it in reflection

Dim NewReport as MyReport

Select Case strReportName
Case = "OrderReport"
NewReport = New OrderReport 'Inherits MyReport
Case = "InvoiceReport"
NewReport = New InvoiceReport 'Inherits MyReport

The list is very long, I wish to just pass the string strReportName to
reflection and
reflection will get the correct class

Thanx in advance
Geri
 
C

Carlos J. Quintero [.NET MVP]

Hi Geri,

Take a look at the System.Activator.CreateInstance(...) methods.

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
You can code, design and document much faster.
Free resources for add-in developers:
http://www.mztools.com
 
H

Herfried K. Wagner [MVP]

ghobley said:
I have a problem where by wish to instantiate an object say
NewReport
to a
specific class, all inherited from the base class MyReport, I have the
classname as a string and I don't want to use a massive Case Statement, I
wish to use Reflection as this is more efficient. This is how I would do
it
in a case statement, how can I do it in reflection

<URL:http://www.google.to/[email protected]>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top