How to call the right constructor

A

Alex D.

Hi guys. I am trying to create a specific object calling its constructor
based on a string containing the name of the object. Let say I have three
objects A, B, and C. Also let say I let the user enter a text in a textbox.
If the user enters 'A' or 'B' I want to do this

if (TextBox1.Text == 'A')
object O = new A();

if (TextBox1.Text == 'B')
object O = new B();

.....and so on.

Since at design time I dont know what objects I have I need to do this in a
generic and dinamic way...is there any way?? I hope so!
Any help would be great.
Thanks,
Alex.
 
M

Mattias Sjögren

Since at design time I dont know what objects I have I need to do this in a
generic and dinamic way...is there any way?? I hope so!

Check out System.Activator.CreateInstance().


Mattias
 
A

Alex D.

Thanks! that's the way to go, though I am still trying to make it work with
my multiple parameters.

Thanks again,
alex.
 

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