GrayFox
GrayFox

Reputation: 1089

Force ObservableCollection<T> collection to get a specific Type

I have this generic method in my base class:

protected void Show<T>(ObservableCollection<T> collection)
{
    StatusText = Resource.loadingData;
    if (collection is ObservableCollection<Language>)
    {
        collection = DBService.GetLanguages();
    }
    StatusText = string.Empty;
}

If the collection's type is Language, I want to force the collection to reference the DBService.GetLanguages()'s return ObservableCollection<Language>.

But the compiler says:

A implicit conversion of "System.Collections.ObjectModel.ObservableCollection<DataModel.Language>" in "System.Collections.ObjectModel.ObservableCollection<T>" is not possible.

Upvotes: 0

Views: 80

Answers (1)

okrumnow
okrumnow

Reputation: 2416

I'm guessing you're doing something with collection inside this method (probably showing the members), but it's definitely no good idea to reassign collection inside the Show method. Check for type Language outside of the method and pass the correct collection to show.

BTW: you need to check T for type language, not the collection.

Upvotes: 2

Related Questions