2

我有这个应用程序,我试图将列表传递给另一个类。我不断收到错误

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

该列表是从 Json 生成的。

登录类

   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; }
        }

我还有另一门课:全球课

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; }
        }
    }
}

我究竟做错了什么?错误在第一堂课中抛出 GlobalClass.Businesses = businesses;

4

2 回答 2

5

您似乎有两个相同的类,称为Business- 一个嵌套在您GlobalClass的内部,另一个嵌套在您的LoginForm.

尽管两个类看起来相同,但它们不能相互分配。此外,基于这些类的集合也不能相互分配。

考虑删除其中一个类(例如 中的私有类LoginForm)并将其所有用途替换GlobalClass.Business为公开的。

于 2013-11-11T18:06:20.810 回答
2

你有两个完全不同的类,它们都被命名为Business. 一个在这里:

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

和一个在这里:

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

尽管这两个类大部分是相同的,但它们静态编译为两个完全不同的东西。再多类似的命名也不会改变这一点。

可以编写方法将一个转换为另一个,但这可能只是隐藏问题而不是实际解决它。我想你想要做的是选择哪一个是你的实际Business课程并只使用它。因此,您可能会弃用第二个并将Response属性更改为:

public List<GlobalClass.Business> Businesses { get; set; }
于 2013-11-11T18:07:06.830 回答