【问题标题】:Accessing Static Variables C# [duplicate]访问静态变量 C# [重复]
【发布时间】:2015-06-12 13:58:49
【问题描述】:

我有一个简单的静态类,用于从应用程序的任何地方访问我的 AVLTree。但是由于某种原因,我无法从另一个类中调用该变量。每当我键入数据库时​​。我只得到两种不是我想要的方法。我想访问 Database.countries 但这是不可能的。

static class Database
{
   static AVLTree<Country> countries = new AVLTree<Country>();

   static Database()
   {
   }

   static AVLTree<Country> cees()
   {
      return countries;
   }

   static AVLTree<Country> Countries
   {
      get { return countries; }
   }
}

【问题讨论】:

  • 如果你想从课堂外访问它们,你的属性应该是public
  • 类成员的默认修饰符是private,因此您需要添加public,例如public static AVLTree&lt;Country&gt; Countries.

标签: c# static avl-tree


【解决方案1】:

您需要公开您的财产

public static class Database
{
    static AVLTree<Country> countries = new AVLTree<Country>();

    static Database()
    {

    }

    static AVLTree<Country> cees()
    {
        return countries;
    }

    public static AVLTree<Country> Countries
    {
        get { return countries; }
    }
}

【讨论】:

  • 愚蠢的错误,非常感谢
猜你喜欢
  • 2013-01-31
  • 1970-01-01
  • 1970-01-01
  • 2010-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-30
  • 2015-07-22
相关资源
最近更新 更多