Search code examples
c#listinterfacereturntypeconverter

Cannot implicitly convert type with interface


I am coding a C# Xamarin application, and am getting the following error:

Error CS0029: Cannot implicitly convert type 'System.Collections.Generic.List<SimpleMapDemo.AndroidMapLocationImageViewModel>' to 'System.Collections.Generic.List<SimpleMapDemo.AndroidIGridViewItemWithAssets>'

I am getting the error at this line:

return androidMapLocationImageGalleryViewModel.images;

In the following function:

public List<AndroidIGridViewItemWithAssets> GetAndroidMapLocationItemGridViewItemsFromWebService(string type, int webServiceId)
{
    if (type.Equals("Image Gallery")) 
    {
        AndroidMapLocationImageGalleryViewModel androidMapLocationImageGalleryViewModel = GetAndroidMapLocationImageGallery (webServiceId);
        return androidMapLocationImageGalleryViewModel.images;
    }
    return null;
}

Here is my AndroidMapLocationImageGalleryViewModel code:

public class AndroidMapLocationImageGalleryViewModel
{
    public int mapLocationItemImageGalleryId { get; set; } //may not be needed
    public List<AndroidMapLocationImageViewModel> images;

    public AndroidMapLocationImageGalleryViewModel ()
    {
        images = new List<AndroidMapLocationImageViewModel> ();
    }   
}

The AndroidMapLocationImageViewModel inherits from AndroidIGridViewItemWithAssets.

Can I please have some help with this code?

Thanks in advance


Solution

  • you need:

    return androidMapLocationImageGalleryViewModel.images
        .Cast<AndroidIGridViewItemWithAssets>()
        .ToList();
    

    Cast method: http://msdn.microsoft.com/en-us/library/bb341406(v=vs.110).aspx