views:

185

answers:

4

Suppose I have a custom control like:

MyControl : Control

if I have code like this:

List<Control> list = new ...
list.Add (myControl);

RolloutAnimation anim = list[0];

I know I can do it at compile time but I wanna do it at runtime and access MyControl specific functionality.

+4  A: 
((MyControl)list[0]).SomeFunction()
Andy West
Thanks but I want to do it at runtime, like list[0].Cast(list[0].GetType()), etc.
Joan Venge
I think you might be confused about what "run time" means. It means "while the program is being executed".
Andy West
I mean something like this:http://stackoverflow.com/questions/345506/whats-the-runtime-equivalent-of-c-bracketed-type-cast
Joan Venge
I think I understand now. The thing I wasn't getting (and I got from Luvieere's answer) is that you don't know what type you'll be casting to at run time. But the actual casting operation happens at run time, regardless.
Andy West
+1  A: 

(MyControl)list[0] will return an object of type MyControl, or throw error if list[0] is not a MyControl.

list[0] as MyControl will return an object of type MyControl, or null if list[0] is not of type MyControl

You can also check type type of list[0] testing list[0] is MyControl

Nestor
Totally offtopic, but I always hated the as Object method. Means I get a null reference exception later all instead of where the actual error occurred (the location of the cast). It has lead to quite a few interesting bug hunts...
Rontologist
Very good point. It's good to remember "as".
Andy West
@Rontologist Not if it's used correctly. But is true a lot of people use it thinking it just "looks nicer" than a cast, and that brings trouble.
Fredy Treboux
A: 
MyControl my_ctrl = list[0] as MyControl;

if(my_ctrl != null)
{
  my_ctrl.SomeFunction();
}

// Or

if(list[0] is MyControl)
{
  ((MyControl)list[0]).SomeFunction();
}
szielenski
+7  A: 

Why do you want to do it at runtime? If you have more types of Controls in your List, that have some specific functionality, but different types, perhaps they should implement a common interface:

interface MyControlInterface
{
    void MyControlMethod();
}

class MyControl : Control, MyControlInterface
{
    // Explicit interface member implementation:
    void MyControlInterface.MyControlMethod()
    {
        // Method implementation.
    }
}

class MyOtherControl : Control, MyControlInterface
{
    // Explicit interface member implementation:
    void MyControlInterface.MyControlMethod()
    {
        // Method implementation.
    }
}


.....

//Two instances of two Control classes, both implementing MyControlInterface
MyControlInterface myControl = new MyControl();
MyControlInterface myOtherControl = new MyOtherControl();

//Declare the list as List<MyControlInterface>
List<MyControlInterface> list = new List<MyControlInterface>();
//Add both controls
list.Add (myControl);
list.Add (myOtherControl);

//You can call the method on both of them without casting
list[0].MyControlMethod();
list[1].MyControlMethod();
luvieere
Thanks but in this case what type should I use for the List? Control or Interface?
Joan Venge
Nice recommendation
Andy West
The list is `List<MyControlInterface>`, so that every control you would add to it is supposed to be an instance of a class implementing `MyControlInterface`. This way, you do not care what the actual class is, just that it implements the interface, and the method you want to call is accessible.
luvieere
Thanks, I will try this.
Joan Venge