473,568 Members | 2,962 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Create Instance Of Class Using text name

I want create an instance of an object which accepts a Data Reader as a
constructor. I realize that I could create a gigantic case statement in
which I test for each text name, but that case statement would be rather
larger and I would have to revise as new classes are added or removed.
Another variable will tell me the namespace that I will find the class in.
So I would have a case statement which would evaluate the variable for
namespace, but what would the code look like to create a class for that text
name.

Example:
string classNm = "Account_Tb l";
switch (myNameSpaces)
{
case "GL.Tables"
Now I want to create and instance of "GL.Tables.Acco unt_Tbl" but
I want to
use the variable "classNm" to create the equivalent of the
statement below:
GL.Tables.Accou nt_Tbl = new GL.Tables.Accou nt(myDataReader );
break;
case "PACS.Table s"
Now I want to create and instance of "PACS.Tables.Ac count_Tbl"
but I want
to use the var "classNm" to create the equivalent of the
statement below:
PACS.Tables.Acc ount_Tbl = new PACS.Tables.Acc ount(myDataRead er);
break;

Feb 5 '07 #1
2 2099
=?Utf-8?B?T2xkQnV0U3R pbGxMZWFybmluZw ==?=
<Ol************ *****@discussio ns.microsoft.co mwrote in
news:C9******** *************** ***********@mic rosoft.com:
I want create an instance of an object which accepts a Data Reader as
a constructor. I realize that I could create a gigantic case
statement in which I test for each text name, but that case statement
would be rather larger and I would have to revise as new classes are
added or removed. Another variable will tell me the namespace that I
will find the class in. So I would have a case statement which would
evaluate the variable for namespace, but what would the code look like
to create a class for that text name.

Example:
string classNm = "Account_Tb l";
switch (myNameSpaces)
{
case "GL.Tables"
Now I want to create and instance of
"GL.Tables.Acco unt_Tbl" but
I want to
use the variable "classNm" to create the equivalent of the
statement below:
GL.Tables.Accou nt_Tbl = new
GL.Tables.Accou nt(myDataReader );
break;
case "PACS.Table s"
Now I want to create and instance of
"PACS.Tables.Ac count_Tbl"
but I want
to use the var "classNm" to create the equivalent of the
statement below:
PACS.Tables.Acc ount_Tbl = new
PACS.Tables.Acc ount(myDataRead er);
break;

System.Activato r has several methods for instantiating objects for you.

For example:
Type myType = Type.GetType("G L.Tables.Accoun t_Tbl");

object[] params = new object[1];
params[0] = myDataReader;

Activator.Creat eInstance(myTyp e, params);
(I haven't tried this, but something like the above should work).

/Peter
Feb 5 '07 #2
Hi,

"OldButStillLea rning" <Ol************ *****@discussio ns.microsoft.co mwrote
in message news:C9******** *************** ***********@mic rosoft.com...
|I want create an instance of an object which accepts a Data Reader as a
| constructor. I realize that I could create a gigantic case statement in
| which I test for each text name, but that case statement would be rather
| larger and I would have to revise as new classes are added or removed.
| Another variable will tell me the namespace that I will find the class in.
| So I would have a case statement which would evaluate the variable for
| namespace, but what would the code look like to create a class for that
text
| name.

Take a look at CreateInstance , depending of where your classes resides, or
how you plan to instantiate them you will have to use the correct class like
Assembly, AppDomain, Activator, etc
Also you should pay attention to the return type, you will have to return
object unless all your classes share a common base
--
Ignacio Machin
machin AT laceupsolutions com
Feb 5 '07 #3

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

Similar topics

6
22500
by: Martin | last post by:
I'd like to be able to get the name of an object instance from within a call to a method of that same object. Is this at all possible? The example below works by passing in the name of the object instance (in this case 'myDog'). Of course it would be better if I could somehow know from within write() that the name of the object instance was...
6
3298
by: SamIAm | last post by:
Hi am creating a email application that needs to mail out a very large amount of emails. I have created a multithreaded c# application that using message queuing. I have created a threadpool of 5 threads and each thread checks the queue, receives the message and sends an email. How do I determine the right amount of threads to create? ...
0
3626
by: Patrick | last post by:
I'm working on a contact management application, and need a hand with one aspect... Here's what I want to create: ------------------------------------ A form split into two parts. There is a datagrid on the left side that lists names and perhaps a couple of other key fields. The user can click on a record in the datagrid, which should...
18
7373
by: Sandra-24 | last post by:
Can you create an instance of a subclass using an existing instance of the base class? Such things would be impossible in some languages or very difficult in others. I wonder if this can be done in python, without copying the base class instance, which in my case is a very expensive object. Any ideas? Thanks,
3
9574
by: eSolTec, Inc. 501(c)(3) | last post by:
Thank you in advance for any and all assistance. I'm trying to create a call to a web page to validate and register software. The code I'm using is: Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click Dim WebRequest As HttpWebRequest Dim instance As HttpWebRequest =...
10
4768
by: Henrik Dahl | last post by:
Hello! I have an xml schema which has a date typed attribute. I have used xsd.exe to create a class library for XmlSerializer. The result of XmlSerializer.Serialize(...) should be passed as the value for the parameter of an SqlCommand for inserting the xml document in a column of a table where the column is typed to be of the same xml...
4
12422
by: etuncer | last post by:
Hello All, I have Access 2003, and am trying to build a database for my small company. I want to be able to create a word document based on the data entered through a form. the real question is this: can Access create the document and place it as an OLE object to the relevant table? Any help is greatly appreciated. Ricky
4
6887
by: Vlad | last post by:
I am having problems using the file.create method within a function that is called when looping through an array of filepaths. If I call my function with a hardcoded file path --C:\Temp.txt the function creates the file as expected. When I loop through my array I get the error - "ArgumentException was unhandled - Illegal characters in...
4
8267
by: sirjohnofthewest | last post by:
If I possessed the power to sway the mind of every user in the world to delete all forms of Internet Explorer I would die a happy man. Hi guys, I frequently visit this site to get answers to my problems and this one is really getting to me... I have a page that allows you to Browse Authors. There are three drop down boxes that auto-populate...
0
7693
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7605
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7665
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7962
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5501
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3631
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1207
muto222
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.