在Visual Studio/Xamarin/ c#中无法识别字典



我在这里迷路了,由于某种原因,我从第10行得到了多个错误。我正在使用Xamarin构建一个应用程序,我试图创建一个字典,出于某种原因,我得到以下错误

Error   IDE1007 The name 'exerciseTypes.Add' does not exist in the current context.
Error   IDE1007 The name 'exerciseTypes' does not exist in the current context.
Error   IDE1007 The name 'Add' does not exist in the current context.   
using System;
using System.Collections.Generic;
using System.Text;
namespace Trainer_App
{
internal class Exercise
{
public static Dictionary<string, bool> exerciseTypes = new Dictionary<string, bool> ();
exerciseTypes.Add("Warmup",false);
private string name;
private int min_num_balls;
private int[] num_participants;
private string court_type;
private string[] accessories;
private static int id = 0;
public Exercise()
{
}
}
}

编辑:我希望字典是类的一部分,而不是对象

不能将方法调用直接置于类声明之下。您可以将它移动到构造函数中:

using System;
using System.Collections.Generic;
using System.Text;
namespace Trainer_App
{
internal class Exercise
{
public Dictionary<string, bool> exerciseTypes = new Dictionary<string, bool> ();
private string name;
private int min_num_balls;
private int[] num_participants;
private string court_type;
private string[] accessories;
private static int id = 0;
public Exercise()
{
exerciseTypes.Add("Warmup",false); // Here!
}
}
}

相关内容

  • 没有找到相关文章

最新更新