Click here to Skip to main content
15,891,881 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Hi I would like to set an class's object on runtime.
I have more than 1 classes and all classes initialized already.

For example,

Class A{
  public string obj1;
  public string obj2;
}
Class B{
  public string obj3;
  public string obj4;
}

I already initialized those object as
A a=new A();
B b=new B();

on form load.

I am getting from database object and class name. let say class name as A and object name is obj1.

I want to set that object on runtime. because there is no only 2 object more than 100 and more then 10 class name is defined. Not good idea for if else.

I can get all object name by using reflection. But no idea about setting.

Is there any one to have idea to set class object on runtime?


Thanks
Posted
Updated 12-May-11 5:51am
v2

I'm not sure why you would need to do it that way. Why not make the "object name" a property of your class, then create your class instances and hold them in some kind of collection (list, etc.)?
 
Share this answer
 
Comments
cmpemikail 12-May-11 16:40pm    
Thanks for reply.
Can you give an example please? It will better to get idea.

Thanks
Sergey Alexandrovich Kryukov 12-May-11 23:24pm    
Total gibberish. OP should read the answer again. Example of what. OP presents a remarkable example of confusion in basic principles of programming. Beyond help, at the moment.
--SA
Marc A. Brown 13-May-11 9:02am    
SA is right. These are very basic concepts. You need to do a little research, maybe find a good book on OO concepts and C#, and you'll figure it out in no time.
Sergey Alexandrovich Kryukov 12-May-11 23:24pm    
My 5.
--SA
Marc A. Brown 13-May-11 9:00am    
Thanks!
Try like that
<br />
A cn = (A)Activator.CreateInstance(A);<br />


It will craete an instance on run time

If you want to set the value to this object then you have to use

<br />
PropertyInfo.SetValue()<br />
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900