By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,835 Members | 1,488 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,835 IT Pros & Developers. It's quick & easy.

Create Instance Of Class Using text name

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
=?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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.