在Visual Studio/Xamarin/C#中未识别字典

发布于 2025-02-10 10:01:09 字数 946 浏览 1 评论 0原文

我在这里迷路了,由于某些原因,我从第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()
        {
        }
    }
}

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

I am lost here, for some reason I am getting multiple errors from line 10. I am building an app using Xamarin and I am trying to create a dictionary and for some reason, I get the following errors

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()
        {
        }
    }
}

Edit: I want the dictionary to be a part of the class and not the Objects

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

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

发布评论

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

评论(1

你又不是我 2025-02-17 10:01:09

您不能将方法调用直接放在类声明下。但是,您可以将其移入构造函数中:

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

You can't place a method call directly under a class declaration. You could move it inside the constructor though:

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!
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文