0

The following code gives the error "Cannot convert type 'Calendar.Month' to 'System.Collections.Generic.List'":

public class Month : List<Day>, INotifyPropertyChanged
{
  public Month() { /* NOP */ }

  public new void Add<Day>(Day pValue)
  {
    var list = (List<Day>)this;

    list.Add(pValue);
  }
}

I have studied the advice here: Why does calling a method in my derived class call the base class method?

and here:

C# Call shadow method with generic cast

I want to override the List.Add(...) method with a Month.Add(...) method to send an event to the target XAML container classes and then call the overridden List.Add(...) method.

By the way, I am using VS2012, .NET 4.5, and the application is for Win Phone 8 (later versions are not presently an option - sorry). I mention this because I know from experience a lot changed between Win Phone 8 and Win 10.

rfreytag
  • 955
  • 1
  • 8
  • 24
  • 4
    I'm sure you mean `base.Add(pValue);` instead of `list.Add(pValue);` – René Vogt May 17 '16 at 12:48
  • 1
    Instead of using inheritance why not use composition? – juharr May 17 '16 at 12:48
  • 1
    Why do you want to inherit from `List` instead of `Collection`? – Dennis May 17 '16 at 12:57
  • Is a Month a List? Not so much. Siding with @juharr on this on. A month has a list of days. – spender May 17 '16 at 13:07
  • I used List instead of Collection because that is how Laurent and Jaime did it in their 'Data Binding' lecture (minutes 40-49) found here: https://mva.microsoft.com/en-US/training-courses/xaml-deep-dive-for-windows-windows-phone-apps-jump-start-8228?l=BIQ8v9Iy_204984382 Were they mistaken? Are List not usable when data binding the contents of an ItemsControl? PS. I am treating Month as a List for the purposes of this question. It is intended to follow the approach taken by Laurent and Jaime. – rfreytag May 17 '16 at 14:21

1 Answers1

2

The problem here is the use of the generic type parameter and all the casting around. Since you already derive your class from the generic List<T> to List<Day>, there is no need to have another type parameter.

Casting is not really useful here. Just use base.:

public new void Add(Day pValue)
{
    base.Add(pValue);
}

And instead of using a custom implementation of List<T>, use an ObservableCollection<T> which does all this for you already.

If you want to hold on to your list, implement IList<T> yourself and use a backing list to store the data. In that class you have total control over what you do when adding, deleting, etc.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • Thanks for your comment. I have tried your suggestions and here is why I found: 1. implementing IList myself works. But the goal here is understanding so I also .. – rfreytag May 17 '16 at 14:51
  • _(the above comment is incomplete)_ Thank you for your comments. I tried each of your suggestions out of curiosity and found: **1.** implementing `IList` compiles and items `Add(...)`. **2.** using `List` and calling using `base.Add(...)` works like above - thanks I just forgot about 'base'. This is what I needed. **3.** `ObservableCollection` compiles but I'm not familiar with it so having more of a struggle than I like replicating a simple behavior like `List.Add(...)`. I don't think it gets me much as I implemented `Month` class using `INotifyPropertyChanged` - right? – rfreytag May 17 '16 at 15:26
  • 1
    Yes, that should do @rfreytag – Patrick Hofman May 17 '16 at 15:45