Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 错误CS0120:非静态字段、方法或属性需要对象引用_C# - Fatal编程技术网

C# 错误CS0120:非静态字段、方法或属性需要对象引用

C# 错误CS0120:非静态字段、方法或属性需要对象引用,c#,C#,我在下面的一行中得到了这个错误消息,但我不知道我做错了什么 Scorelist.Add(entry.StatValue); 错误CS0120:非静态对象需要对象引用 字段、方法或属性“Game1.Scorelist” 我如何解决这个问题 public List<int> Scorelist = new List<int>(); NewClient(); public async void NewClient() { await DoRe

我在下面的一行中得到了这个错误消息,但我不知道我做错了什么

 Scorelist.Add(entry.StatValue);
错误CS0120:非静态对象需要对象引用 字段、方法或属性“Game1.Scorelist”

我如何解决这个问题

  public List<int> Scorelist = new List<int>();

  NewClient();

  public async void NewClient()
  {
      await DoReadLeaderboard();
  }

    private static async Task DoReadLeaderboard()
    {
        // Get Leaderboard Request
        var result = await PlayFabClientAPI.GetLeaderboardAsync(new GetLeaderboardRequest()
        {
            // Specify your statistic name here
            StatisticName = "TestScore",
            // Override Player Profile View Constraints and fetch player DisplayName and AvatarUrl
            ProfileConstraints = new PlayerProfileViewConstraints()
            {
                ShowDisplayName = true,
                ShowAvatarUrl = true
            }
        });


        if (result.Error != null)
        {
            // Handle error if any
            Console.WriteLine(result.Error.GenerateErrorReport());
        }
        else
        {
            // Traverse the leaderboard list
            foreach (var entry in result.Result.Leaderboard)
            {                  
                Scorelist.Add(entry.StatValue);
            }
        }
    }

您使用的是thje属性Scorelist,但这不是一个静态属性,并且您没有该类的实例。您可能认为,因为您是从成员方法调用它,所以它是可以的,但因为这个方法是静态的,所以它不是。您有3种解决方案:

1使DoReadBoard非静态:

private async Task DoReadLeaderboard()
2使记分表保持静态:


DoReadLeadboard是一个静态方法,但Scorelist是一个类属性。DoReadLeadboard需要是静态的有什么原因吗?可能是@Jason的重复,这是本教程中的代码。我不确定它是否应该是静态的。好吧,这段代码并不是试图修改实例变量。FWIW这是基本的C,与XamarinThanx无关。私有异步任务DoReadBoard有效。
public static List<int> Scorelist = new List<int>();
var instance = new <<ClassName>>();
instance.Scorelist.Add(entry.StatValue);