class name

A

Andrew

Hi,

I have a class named "clsTest"
Is it possible to capture the value of clsTest in a variable, and then use
it to instantiate the object A ?

if (ClassName = "clsTest1")
then
ClassName A = new ClassName;
else if (ClassName = "clsTest2 ")
ClassName A = new ClassName;
//normally this line would be clsTest2 A = new clsTest2();

Does this make sense ? Perhaps my logic is screwed.
Thanks for your reply.
regards
Andrew
 
L

Lasse Vågsæther Karlsen

Andrew said:
Hi,

I have a class named "clsTest"
Is it possible to capture the value of clsTest in a variable, and then use
it to instantiate the object A ?

if (ClassName = "clsTest1")
then
ClassName A = new ClassName;
else if (ClassName = "clsTest2 ")
ClassName A = new ClassName;
//normally this line would be clsTest2 A = new clsTest2();

Does this make sense ? Perhaps my logic is screwed.
Thanks for your reply.
regards
Andrew

Look at the "Activator" class.
 
I

Ignacio Machin ( .NET/ C# MVP )

Hi,

I have a class named "clsTest"
Is it possible to capture the value of clsTest in a variable, and then use
it to instantiate the object A ?

if (ClassName = "clsTest1")
then
  ClassName A = new ClassName;
else if (ClassName = "clsTest2 ")
   ClassName A = new ClassName;
//normally this line would be clsTest2 A = new clsTest2();

Does this make sense ? Perhaps my logic is screwed.
Thanks for your reply.
regards
Andrew

Depend, are clsTest1 and clsTest2 related? or have a common interface?
if so you can write it almost similar to the one you have now.

If they dont have a common interface then you will have to use
Reflection to access the members.


If you explain in more details wht you want we can give you a better
idea though.
 
A

Andrew

Thanks for your replies.
For example, I have a form from which the user chooses A or B, and a
different class will be called each time.
if (user="A")
{ ClassName = "clsTest1";
ClassName A = new ClassName; }
else if (user="B")
{ ClassName = "clsTest2 ";
ClassName A = new ClassName; }
//normally this line would be clsTest2 A = new clsTest2();

I have a 2 versions of a "Process" class which processes some data.
Depending on which choice the user has, the version of the "Process" class
will be instantiated. Perhaps I should create an interface between this Form
and the "Process" classes, but I think I will still have this similar problem.

I read up on "Activator" class and I am not sure which method I should be
using.
This line of code gets an error.

eg. Object o = Activator.CreateInstance(typeof(ClassName ));
System.Type type = o.GetType();
Error: The Type or Namespace name "ClassName" cannot be found.

I hope this is clear.
I'm haven't used Reflection before, can you point me in the right direction ?

cheers
Andrew
 
J

Jon Skeet [C# MVP]

Thanks for your replies.
For example, I have a form from which the user chooses A or B, and a
different class will be called each time.
if (user="A")
   { ClassName = "clsTest1";
         ClassName A = new ClassName; }
else if (user="B")
   { ClassName = "clsTest2 ";
    ClassName A = new ClassName; }
//normally this line would be clsTest2 A = new clsTest2();

I have a 2 versions of a "Process" class which processes some data.
Depending on which choice the user has, the version of the "Process" class
will be instantiated. Perhaps I should create an interface between this Form
and the "Process" classes, but I think I will still have this similar problem.

I read up on "Activator" class and I am not sure which method I should be
using.
This line of code gets an error.

eg. Object o = Activator.CreateInstance(typeof(ClassName ));
     System.Type type = o.GetType();
Error: The Type or Namespace name "ClassName" cannot be found.

I hope this is clear.
I'm haven't used Reflection before, can you point me in the right direction ?

Well, you *could* use Type.GetType instead of typeof to get the name,
but it would be better to use typeof when you're converting the user
input:

switch (user)
{
case "A":
typeToCreate = typeof(ClassA);
break;
case "B":
typeToCreate = typeof(ClassB);
breal;
...
}

although at that point I'd consider creating a Dictionary<string,Type>
instead, and just doing a simple lookup.

Jon
 
A

Andrew

Thanks for your input. But I think you mis-understand me. I wouldn't know the
value of ClassName. This is my code so far.
code:
===
string classA = Properties.Settings.Default.ClassName;
classA className = new classA();
Is it possible to substitute "classA" with a, say "string" because that
value is a variable depending on the ApplicationSetting value ?

I tried to get the Type of the string hoping that it could let me to create
the class. But I don't think I am on the right track. There are debugging
errors....
System.Type typeToCreate = typeof(classA);
typeToCreate A = new typeToCreate();

cheers
Andrew
 
I

Ignacio Machin ( .NET/ C# MVP )

Thanks for your input. But I think you mis-understand me. I wouldn't know the
value of ClassName. This is my code so far.
code:
===
  string classA = Properties.Settings.Default.ClassName;
  classA className = new classA();
Is it possible to substitute "classA" with a, say "string" because that
value is a variable depending on the ApplicationSetting value ?

Yes, follow Lasse's suggestion and take a look at Activator class
 

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