C# – Cannot implicitly convert type ‘System.Collections.Generic.List

cjson

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;

Best Answer

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.