473,394 Members | 1,854 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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_Tbl";
switch (myNameSpaces)
{
case "GL.Tables"
Now I want to create and instance of "GL.Tables.Account_Tbl" but
I want to
use the variable "classNm" to create the equivalent of the
statement below:
GL.Tables.Account_Tbl = new GL.Tables.Account(myDataReader);
break;
case "PACS.Tables"
Now I want to create and instance of "PACS.Tables.Account_Tbl"
but I want
to use the var "classNm" to create the equivalent of the
statement below:
PACS.Tables.Account_Tbl = new PACS.Tables.Account(myDataReader);
break;

Feb 5 '07 #1
2 2091
=?Utf-8?B?T2xkQnV0U3RpbGxMZWFybmluZw==?=
<Ol*****************@discussions.microsoft.comwrot e in
news:C9**********************************@microsof t.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_Tbl";
switch (myNameSpaces)
{
case "GL.Tables"
Now I want to create and instance of
"GL.Tables.Account_Tbl" but
I want to
use the variable "classNm" to create the equivalent of the
statement below:
GL.Tables.Account_Tbl = new
GL.Tables.Account(myDataReader);
break;
case "PACS.Tables"
Now I want to create and instance of
"PACS.Tables.Account_Tbl"
but I want
to use the var "classNm" to create the equivalent of the
statement below:
PACS.Tables.Account_Tbl = new
PACS.Tables.Account(myDataReader);
break;

System.Activator has several methods for instantiating objects for you.

For example:
Type myType = Type.GetType("GL.Tables.Account_Tbl");

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

Activator.CreateInstance(myType, params);
(I haven't tried this, but something like the above should work).

/Peter
Feb 5 '07 #2
Hi,

"OldButStillLearning" <Ol*****************@discussions.microsoft.comwrot e
in message news:C9**********************************@microsof t.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
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...
6
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...
0
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...
18
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...
3
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...
10
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...
4
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...
4
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...
4
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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...

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.