Click here to Skip to main content
15,889,858 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Dear Friends,
I am trying to cast object obj type, into one of data types like dataset, toolstriptextbox or toolstripcombobox. according to the type of obj. Can you please suggest how can I dynamically cast the obj in one of these types. what is the best thing in place of '?' so the obj is easily casted to the best suitable type.
C#
if (obj is DataSet || obj is ToolStripTextBox || obj is ToolStripComboBox )
                  {
                      ListOfControls.Add(obj);
                      serializablearray.Add(((?)obj).Name);
                  }

Thank You
Posted

A dataset is not a control. Use strongly typed lists where ever possible. Use the as keyword to convert your objects so you can insert them as a strongly typed object. Your list is semi useless, those items don't even have a common base class, so they still need casting to do anythign with.
 
Share this answer
 
If you simply want to store the type name, just do this:

ListOfControls.Add(obj);
serializablearray.Add(obj.GetType().Name);


You can optionally use obj.GetType().FullName if you want to include the fully qualified namespace path.
 
Share this answer
 

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