Kinyanjui Kamau
Kinyanjui Kamau

Reputation: 1986

Cannot implicitly convert type 'System.Collections.Generic.List

I have this application where I am trying to pass a list to another class. I keep getting the error

Cannot implicitly convert type 'System.Collections.Generic.List<eko_app.LoginForm.Business>' to 'System.Collections.Generic.List<eko_app.GlobalClass.Business>'

The list is generated from Json.

Login class

   private void Connect()
        {
            try
            {
                serverUrl = "https://eko-app.com/Users/login/username:" + usernameEntered + "/password:" + passwordEntered + ".json";

                var w = new WebClient();
                var jsonData = string.Empty;

                // make the login api call
                jsonData = w.DownloadString(serverUrl);

                if (!string.IsNullOrEmpty(jsonData))
                {
                    // deserialize the json to c# .net
                    var response = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(jsonData);

                    // Get the sessionId
                    GlobalClass.SessionId = string.Empty;
                    GlobalClass.SessionId = response.response.SessionId;

                    if (GlobalClass.SessionId != null)
                    {                      
                        var businesses = response.response.Businesses;

                        GlobalClass.Username = "";
                        GlobalClass.Username = usernameEntered;                        

                        GlobalClass.Businesses = null;
                        GlobalClass.Businesses = businesses; **<-----error thrown here**

                        HomeForm homeForm = new HomeForm();
                        this.Hide();
                        homeForm.Show();
                    }
                    else
                    {
                        Console.WriteLine("Login failed");
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private class Business
        {
            public string businessID { get; set; }
            public string businessName { get; set; }
        }

        private class Response
        {
            [JsonProperty("sessionId")]
            public string SessionId { get; set; }

            [JsonProperty("businesses")]    
            public List<Business> Businesses { get; set; }
        }

        private class Messages
        {
            public string sessionId { get; set; }
            public List<Business> businesses { get; set; }
        }

        private class RootObject
        {
            public Response response { get; set; }
            public Messages messages { get; set; }
        }

I have another class: Global Class

namespace eko_app
{
    static class GlobalClass
    {
       ...some code     

        public class Business
        {
            private string businessID { get; set; }
            private string businessName { get; set; }
        }

        private static List<Business> businesses = null;

        public static List<Business> Businesses
        {
            get { return businesses; }
            set { businesses = value; }
        }
    }
}

What am I doing wrong? The error is thrown in the first class at GlobalClass.Businesses = businesses;

Upvotes: 2

Views: 9196

Answers (2)

David
David

Reputation: 219077

You have two completely different classes, both of which happen to be named Business. One here:

static class GlobalClass
{
    public class Business
    {
        private string businessID { get; set; }
        private string businessName { get; set; }
    }
}

and one here:

public class LoginForm
{
    private class Business
    {
        public string businessID { get; set; }
        public string businessName { get; set; }
    }
}

Even though these two classes are mostly identical, they statically compile to two completely different things. No amount of similar naming will change that.

You could write methods to convert one to another, but that will probably just hide the problem instead of actually solve it. I imagine what you want to do is choose which one is your actual Business class and use only that. So you might deprecate the second one and change the Response property to something like:

public List<GlobalClass.Business> Businesses { get; set; }

Upvotes: 2

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 727047

It appears that you have two identical classes called Business - one nested inside your GlobalClass, and the other nested inside your LoginForm.

Although both classes look the same, they are not assignable to each other. Moreover, collections based on these classes are not assignable to each other either.

Consider dropping one of these classes (e.g. the private one in the LoginForm) and replace all its uses with GlobalClass.Business, which is public.

Upvotes: 5

Related Questions