简体   繁体   English

在C#中使用Web服务-List

[英]Consuming web service in C# -List

score#UPDATED# This probably sounds pretty dumb but I've been stuck on this problem for three days now! score#UPDATED#这听起来似乎很愚蠢,但是我已经在这个问题上停留了三天了! I am new to C#. 我是C#的新手。

I am trying to consume a web service in C#. 我正在尝试使用C#使用Web服务。 I managed to consume the web service in Java with the following lines of code: 我使用以下代码行成功使用了Java中的Web服务:

            List x = new ArrayList<Score>();

            x = topScores();

            System.out.println("x" + x.size());

            System.out.println("TOP SCORES:");
            for (Object o : x) {
                Score s1 = (Score) o;
                System.out.println(s1.getScore());

            }

I need to display a list of scores but can't seem to figure it out in C#. 我需要显示一个分数列表,但似乎无法在C#中弄清楚。 Here is where I am stuck in C#: 这是我陷入C#的地方:

           List<object[]> list = new List<score[]>();

I have tried loads of different variations of declaring collections but can't get past this. 我已经尝试了许多不同的声明集合的变体,但是无法超越。 I think I know how to iterate through a list but I can't get past declaring it. 我想我知道如何遍历列表,但是我无法不声明它。 Here is my error message: 这是我的错误信息:

Cannot implicitly convert type `System.Collections.Generic.List<score>' to `System.Collections.Generic.List<object>'

If I change the declaration to : 如果我将声明更改为:

List<score> list = new List<score>();
list = service.TopScores();

I get the same error but this time I get a message saying: 我收到相同的错误,但是这次我收到一条消息,说:

"Object[] TopScores()"

What is the C# equivalent of my Java snippet? 我的Java代码段的C#等效项是什么? I'd really appreciate any help or suggestions! 我真的很感谢任何帮助或建议! Thank you for your time! 感谢您的时间!

The List<> class is in a category of collections called Generics, which allows you to create an array of strongly-typed objects. List <>类位于称为Generics的集合的类别中,该类使您可以创建强类型对象的数组。 . The List part implies an array, so you don't need to declare the type as an array as well. List部分暗含一个数组,因此您也不需要将类型声明为数组。 Instead of List<object[]> , you should just do List<object> . 代替List<object[]> ,您应该只执行List<object> You also can't change the data type of the list. 您也不能更改列表的数据类型。 If you want to create a list of Score you should just do. 如果要创建Score列表,则应该这样做。

List<Score> scores = new List<Score>();

then you could populate the list with scores like 那么您可以用诸如

Score newScore = new Score();
scores.Add(newScore); 

or whatever 管他呢

Thank you for your help...I figured it out... 谢谢您的帮助...我知道了...

        LoginService service = new LoginService();
        List<score> listA = new List<score>();

        object[] array = listA.ToArray();
        array = service.TopScores();

It looks like you don't need to create a List<object> because your code always knows that the elements of the list are of type Score . 看起来您不需要创建List<object>因为您的代码始终知道列表的元素是Score类型。 You can do this instead 你可以这样做

List<Score> list = new List<Score>(); // Not really necessary because you assign it a different value in the next line.

list = topScores().Select(o => o.ToString()).ToList();

...

System.Console.WriteLine("x{0}", list.Length);

System.Console.WriteLine("TOP SCORES:");
foreach (var score in list) {
    System.Console.WriteLine(score.GetScore());
}

However if you really want to create a List<object> from a List<string> the easiest way to do that is this: 但是,如果您真的想从List<string>创建List<object> ,最简单的方法是:

List<string> stringList = ...
List<object> objectList = stringList.ToList<object>();

You need to have the same type when declaring this list. 声明此列表时,您需要具有相同的类型。

List<score> list = new List<score>();

then 然后

    foreach(Score score in list)
    {
    //do something
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM