0

I have some trouble with my abstract generic class:

public abstract class ViewModel<TPrimaryModel> : ViewModel
    where TPrimaryModel : TwoNames, new()
{
    private ObservableCollection<ViewModel<TPrimaryModel>> _searchResults = new ObservableCollection<ViewModel<TPrimaryModel>>();

    public ObservableCollection<ViewModel<TPrimaryModel>> SearchResults
    {
        get { return _searchResults; }
        set
        {
            if (_searchResults != value)
            {
                _searchResults = value;
                RaisePropertyChanged("SearchResults");
            }
        }
    }
}

My concrete class is:

public class RecipientViewModel : ViewModel<Recipient>
{
    protected override void Find()
    {
        try
        {
            SelectedSearchResult = null;
            //Following row leads to an error
            SearchResults = new ObservableCollection<RecipientViewModel>();
            ...
        }
    }
}

This row leads to an error:

SearchResults = new ObservableCollection<RecipientViewModel>(); 

Compiler message is:

Cannot implicitly convert type 
'System.Collections.ObjectModel.ObservableCollection<...RecipientViewModel>' in 
'System.Collections.ObjectModel.ObservableCollection<...ViewModel<...Recipient>>'

Recipient is a model and inherits from TwoNames.

mosquito87
  • 4,270
  • 11
  • 46
  • 77
  • well, you could do `SearchResults = new ObservableCollection>();`. Actually, you shouldn't expose setters for your collections. I think it will make it a lot easier for you if you let the class that owns the lists take care of creating them. subclasses and other users can access the getter and still add/delete/clear etc. – default Dec 18 '14 at 09:38
  • [this looks related](http://stackoverflow.com/questions/12324020/cannot-implicitly-convert-derived-type-to-its-base-generic-type) – default Dec 18 '14 at 09:43
  • [as well as this](http://stackoverflow.com/questions/2765924/implict-type-cast-in-generic-method) – default Dec 18 '14 at 09:45

0 Answers0