none
Pass a derived class to a function that waits an abstract class RRS feed

  • Question

  • Hi everyone,

    I am encountering problem with an abstract class. Here is my problem (in its simplified version) :
    I have an abstract class named Animal and two derived classes named Dog and Cat.
    In the abstract class, I have a static method like this :

    public static FillList(ObservableCollection<Animal> oc, Dictionnary<object, Animal> dic) { ... }

    I would like to call this static method from the derived class with an ObservableCollection of this class and a Dictionnary with this derived class as value like this :

    Dog.SetList(new ObservableCollection<Dog>(), new Dictionnary<Int16, Dog>())

    Of course, it does not work because the method waits the abstract class as argument. Is there a way to make the static method work without needing to modify it.

    Actually, I modified my method like this :

    public static FillList(object, object)

    I cast the objects with a method of the class Dog or Cat but I would like to find another way.

    Thanks for your suggestions



    • Edited by NicolasC Tuesday, November 6, 2012 3:51 PM
    Tuesday, November 6, 2012 3:48 PM

Answers

  • I assume that you mean "wants" rather than "waits" because I don't see any waiting that's going on here at all.

    Of course it makes sense that you cannot pass an ObservableCollection<Dog> to a method that accepts an ObjeservableCollection<Animal>.  You could add a Cat to an ObjeservableCollection<Animal>, but if it's really an ObservableCollection<Dog> adding a Cat to it would be very bad.

    You'll need to make the method generic:

    public static void FillList<T>(ObservableCollection<T> oc, Dictionnary<object, T> dic)
        where T : Animal
        { ... }
    And of course by that same logic you can't have the key of the dictionary be 'object' in the method parameter and 'short' when you call it.  Either find something that is appropriate for the method name and all callers, or make a second generic argument for the dictionary key.

    • Marked as answer by NicolasC Tuesday, November 6, 2012 5:51 PM
    Tuesday, November 6, 2012 4:10 PM

All replies

  • I assume that you mean "wants" rather than "waits" because I don't see any waiting that's going on here at all.

    Of course it makes sense that you cannot pass an ObservableCollection<Dog> to a method that accepts an ObjeservableCollection<Animal>.  You could add a Cat to an ObjeservableCollection<Animal>, but if it's really an ObservableCollection<Dog> adding a Cat to it would be very bad.

    You'll need to make the method generic:

    public static void FillList<T>(ObservableCollection<T> oc, Dictionnary<object, T> dic)
        where T : Animal
        { ... }
    And of course by that same logic you can't have the key of the dictionary be 'object' in the method parameter and 'short' when you call it.  Either find something that is appropriate for the method name and all callers, or make a second generic argument for the dictionary key.

    • Marked as answer by NicolasC Tuesday, November 6, 2012 5:51 PM
    Tuesday, November 6, 2012 4:10 PM
  • Hi,

    Yes, I wanted to say "wants" instead of "waits" (my English is not perfect).
    For the dictionnary, sometimes, it will be a short and sometimes a int. I will create two functions (I think I don't have really the choice).

    Thanks a lot for your answer. It was exactly that I was looking for.

    Have a nice day.

    N.

    Tuesday, November 6, 2012 5:51 PM