Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Convert type List<Object> to Object

I have the following class:

public class Up
{
    public string field { get; set; }
    public string oft { get; set; }
}

and I am calling the following function:

private Up GetUp()
    {
        var json = _fileSystem.File.ReadAllText(localPath);
        var cur = JsonConvert.DeserializeObject<List<Up>>(json);
        //Console.WriteLine(String.Join("\n", current)); 
        return cur;
    }

This gives an error:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Cannot implicitly convert type 'System.Collections.Generic.List<Up>' to 'Up'.

I am trying to return one object with multiple values so I can iterate over them. Is this even possible what I am trying to do here? (fairly new to C#).

>Solution :

When you declare a method, its method signature defines the expected return type (among other things). The error you are seeing is letting you know that you currently have a System.Collections.Generic.List<Up>, but it (the compiler) was expecting Up to be returned by the method.

You are currently defining your method as:

private Up GetUp()

Which is telling the compiler that you want to define a method named GetUp, which is a private method, and which returns an Up object.

You probably want it’s return type to be a List<Up>:

private List<Up> GetUp()

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading