views:

104

answers:

8

This code is a representation of my logic. Obviously it does not work. Please advise on how to make it work.

        switch (ObjectToControl.GetType().ToString())
        {
            case "DataList":
                DataList Controled = (DataList)ObjectToControl;
                break;
            case "DetailsView":
                DetailsView Controled = (DetailsView)ObjectToControl;
                break;
            case "FormView":
                FormView Controled = (FormView)ObjectToControl;
                break;
            case "ListView":
                ListView Controled = (ListView)ObjectToControl;
                break;
            default:
                GridView Controled = (GridView)ObjectToControl;
                break;
        }
A: 

If the originating type is unknown and can't be discovered then there's no valid conversion you can do. Having said that, that should never happen.

As ObjectToControl.GetType().ToString() returns the fully qualified name of the class, so your code will always hit the default case as it stands.

Can you be more specific about the nature of your problem? Is it compile time or run-time for example?

ChrisF
+1  A: 

replace

ObjectToControl.GetType()

with

ObjectToControl.GetType().Name

if that's what you're trying to do.

devnull
Until a `CompletelyDifferent.DataList` comes along.
Pete Kirkham
A: 

ObjectToControl.GetType().ToString() will return the namespace + class name. Maybe that's why you don't get hits in your switch.

AZ
+1  A: 

GetType().ToString() returns the fully-qualified namespace. A "ListView" will be returned as "System.Windows.Forms.ListView".

Jim H.
+3  A: 

Why convert it at all?

All these types inherit from Control. Just type the return value of the method as Control, and cast the object to Control.

return ObjectToControl as Control;

If, somewhere else, you need to execute functionality that only one of the types listed can handle, then at that point, in that method, check to ensure that it is of the proper type before casting and calling the methods which only exist on that specific type..

   if (objecttoControl is TypeToCheck)
       (objecttoControl as TypeToCheck).[MethodName](parameters);

or..

   TypetoCheck myObj = objecttoControl as TypeToCheck;
   if(myObj != null) myObj.[MethodName](parameters);

The first approach is to be preferred when there is a strong possibility of getting objects that are not instances of TypeToCheck (as in the scenario this question poses). The second approach is better when the situation is such that you expect only instances of TypeToCheck to get to that line of code...

Charles Bretana
+1  A: 

Try this:

    object Controled = null; // Default is that we don't know what this is
    if (ObjectToControl is DataList)
        Controled = ObjectToControl as DataList;
    else if (ObjectToControl is DetailsView)
        Controled = ObjectToControl as DetailsView;
    else if (ObjectToControl is FormView)
        Controled = ObjectToControl as FormView;
    else if (ObjectToControl is ListView)
        Controled = ObjectToControl as ListView;
    else if (ObjectToControl is GridView)
        Controled = ObjectToControl as GridView;
GalacticJello
+1  A: 

Another way of doing this is using the "is" keyword:

if(ObjectToControl is DataList)
{
  DataList Controled = (DataList)ObjectToControl;
  //Do something specific to your data list
}
else if(ObjectToControl is DetailsView)
{
  DetailsView Controled = (DetailsView)ObjectToControl;
  //Do something specific to your details view
}
//etc

This tends to be a code smell implying you have an issue with your design though if you don't know the type of class up front.

Paolo
Fails; `Controled` falls out of scope after the `if` blocks.
Zano
True - corrected.
Paolo
+1  A: 

Try in this way

if (ObjectToControl is CheckBox){

    ((CheckBox)ObjectToControl).IsChecked = true;
    //Code Here
}

else if (ObjectToControl is Textbox){

    //Code Here
}

This will avoid namespace confusion.

Thomas M.