473,480 Members | 1,498 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Using reflection to instantiate a class

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
Nov 21 '05 #1
2 5086
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

"ghobley" <gh*****@discussions.microsoft.com> escribió en el mensaje
news:D5**********************************@microsof t.com...
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

Nov 21 '05 #2
"ghobley" <gh*****@discussions.microsoft.com> schrieb:
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/groups?selm=Or5f9nXBFHA.2180%40TK2MSFTNGP12.phx.gb l>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
22048
by: Prashant | last post by:
Hi , Need some help ! . I am storing Classnames in an XML file and depending on the user's choice of process want to : 1) Read the classname of that process from the XML file 2) Instantiate...
1
3203
by: Jeff Molby | last post by:
Sorry for the crossposting guys. I figured this one might be a little tricky, so I'm calling upon the C# brains out there to help out this poor VB developer. I know enough C# to translate any code...
8
16847
by: Robert W. | last post by:
I've almost completed building a Model-View-Controller but have run into a snag. When an event is fired on a form control I want to automatically updated the "connnected" property in the Model. ...
6
1905
by: Simon Verona | last post by:
I would normally use code such as : Dim Customer as new Customer Dim t as new threading.thread(AddressOf Customer.DisplayCustomer) Customer.CustomerId=MyCustomerId t.start Which would create...
3
2317
by: HL | last post by:
The requirement is to send some information to other objects. The objects to whom the information has to be sent is not available at compile time. The names of the types (objects) will be provided...
5
4555
by: Anders Borum | last post by:
Hello! Whilst refactoring an application, I was looking at optimizing a ModelFactory with generics. Unfortunately, the business objects created by the ModelFactory doesn't provide public...
7
2187
by: BK | last post by:
I've used reflection quite a bit to launch forms, it works great, requires little coding, and allows me to launch forms from a dynamic menu. Now I have a need to instantiate any one of several...
1
2822
by: learning | last post by:
Hi how can I instaltiate a class and call its method. the class has non default constructor. all examples i see only with class of defatul constructor. I am trying to pull the unit test out from...
8
3844
by: =?Utf-8?B?U2hhd24=?= | last post by:
Hi; i just started research reflection and i'm wondering if i have an empty class file can i use reflection to add member variables and attributes dynamically and then instantiate the class? What...
0
7044
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6908
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7045
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7087
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6741
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6944
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
2985
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
563
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.