缺少装配参考?

发布于 2024-10-18 07:51:25 字数 885 浏览 2 评论 0原文

我有以下示例方法:

namespace Postcode_webservice
{    
    public class Business
    {
      public string getBusinessDossierno(string KVKnr)
        {
              StringBuilder resultaat = new StringBuilder();
              result = myserviceBusiness.businessGetDossierV3(KVKnr);

              string city = result.results[0].CorrespondenceCity;
              string postcode = result.results[0].CorrespondencePostcode;

              resultaat.Append(city);
              resultaat.Append(postcode);
              return resultaat.ToString();
        }
    }

    public class BusinessInfo
    {
    public string City { get; set; }
    public string PostCode { get; set; }
    public string bedrijfsNaam { get; set; }
    public string adres { get; set; }
    public int huisnr { get; set; }
    }
}

这会导致程序集引用错误。 (已添加使用 System.Collections.Generic)

I have following example method:

namespace Postcode_webservice
{    
    public class Business
    {
      public string getBusinessDossierno(string KVKnr)
        {
              StringBuilder resultaat = new StringBuilder();
              result = myserviceBusiness.businessGetDossierV3(KVKnr);

              string city = result.results[0].CorrespondenceCity;
              string postcode = result.results[0].CorrespondencePostcode;

              resultaat.Append(city);
              resultaat.Append(postcode);
              return resultaat.ToString();
        }
    }

    public class BusinessInfo
    {
    public string City { get; set; }
    public string PostCode { get; set; }
    public string bedrijfsNaam { get; set; }
    public string adres { get; set; }
    public int huisnr { get; set; }
    }
}

This result in an assembly reference error. (using System.Collections.Generic has been already added)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

诠释孤独 2024-10-25 07:51:25

更好取决于您需要使用它做什么。如果您要传递此客户端来填充列表框,那么我更喜欢 json。但是,如果要在服务器端使用它,我会坚持使用 .Net 对象。

作为旁注,为了消除连接字符串来存储数据的愿望,我将定义一个包含城市和邮政编码的类型,如下所示:

public class MyAddressInfo
{
    public string City { get; set; }
    public string PostCode {get; set; }
}

然后使用这些的数组(或列表):

List<MyAddressInfo> myList = new List<MyAddressInfo>();
foreach(var res in result.results)
{
    myList.Add(new MyAddressInfo
    {
        City = res.CorrespondenceCity,
        PostCode = res.CorrespondencePostcode
    });
}

然后将列表返回为如上所述。如果您需要返回一个数组,您可以这样做:

return myList.ToArray();

这将是MyAddressInfo[]的返回类型

@Thomas:
根据您的评论,您的方法声明应如下所示:

public MyAddressInfo[] getBusinessDossierno(string KVKnr)
{
   // etc.
   return myList.ToArray();
}

当您像这样编译它时,您看到的其他错误是什么?

Better depends on what you need to use it for. If you are passing this client-side to populate the listbox, then I would prefer json. However, if it is to be used server-side, I would stick to .Net objects.

As a side note, to eliminate the desire to concatenate strings to store data, I would define a type that contains both city and postal code, like this:

public class MyAddressInfo
{
    public string City { get; set; }
    public string PostCode {get; set; }
}

and then use an array (or List) of these:

List<MyAddressInfo> myList = new List<MyAddressInfo>();
foreach(var res in result.results)
{
    myList.Add(new MyAddressInfo
    {
        City = res.CorrespondenceCity,
        PostCode = res.CorrespondencePostcode
    });
}

And then return the list as described above. If you need to return an array, you can do this:

return myList.ToArray();

which would be a return type of MyAddressInfo[]

@Thomas:
Per your comment, your method declaration should look like this:

public MyAddressInfo[] getBusinessDossierno(string KVKnr)
{
   // etc.
   return myList.ToArray();
}

What are the other errors that you see when you compile it like this?

孤者何惧 2024-10-25 07:51:25

为什么不从方法中返回 KeyValuePair (或更合适的数据结构)列表?

返回一个由城市名称和邮政编码组成的连接字符串是一种混乱的解决方案,并且通过返回一组连接值也不会变得更整洁。

return result.results.Select(r => new KeyValuePair<string, string>(r. CorrespondenceCity, r.CorrespondencePostcode));

Why not return a list of KeyValuePair<string, string> (or some more appropriate data structure) out of the method?

Returning one conjoined string of city names and zip codes is kind of a messy solution and it's not going to get any neater by returning an array of conjoined values.

return result.results.Select(r => new KeyValuePair<string, string>(r. CorrespondenceCity, r.CorrespondencePostcode));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文