【问题标题】:Inconsistent Accessibility on field types in C#C# 中字段类型的可访问性不一致
【发布时间】:2011-11-23 22:05:58
【问题描述】:

我正在尝试为我的程序创建一些简单的类,但我被 Tile[,] 类“theGrid”对象的字段类型不一致的可访问性错误难住了。我已经查看了其他一些解决方案,并将所有内容都设置为公开,但我仍然不知道该怎么做。

你能告诉我如何解决这个问题吗?

public class Level
{
    public Tile[,] theGrid;
    public Tile[,] TheGrid
    {
        get { return theGrid; }
        set { theGrid = value;}
    }

    public static Tile[,] BuildGrid(int sizeX, int sizeY)
    {
        Tile earth = new Tile("Earth", "Bare Earth. Easily traversable.", ' ', true);
        //this.createTerrain();

        for (int y = 0; y < sizeY; y++)
        {
            for (int x = 0; x < sizeX; x++)
            {
                theGrid[x, y] = earth;
            }
        }
        return theGrid;
    }

这是 tile 类的简化版本:

public class Tile
{
    //all properties were set to public

    public Tile()
    {
        mineable = false;
        symbol = ' ';
        traversable = true;
        resources = new List<Resource>();
        customRules = new List<Rule>();
        name = "default tile";
        description = "A blank tile";
        area = "";
    }

    public Tile(string tName, string tDescription, char tSymbol, bool tTraversable)
    {
        resources = new List<Resource>();
        customRules = new List<Rule>();
        area = "";
        symbol = tSymbol;
        this.traversable = tTraversable;
        this.name = tName;
        this.description = tDescription;

        mineable = false;
    }

    public void setArea(string area)
    {
        this.area = area;
    }
}

如果你能在这方面给我任何帮助,我将不胜感激。

【问题讨论】:

  • 你能显示你的Tile类的属性/字段吗?

标签: c# types field


【解决方案1】:

静态方法只能访问静态成员。

您需要创建新的瓷砖数组

 public static Tile[,] BuildGrid(int sizeX, int sizeY)         
 {              
      Tile[,] theGrid = new Tile[sizeX, sizeY];

      .... the rest of the code is the same
 }

【讨论】:

  • 这个。静态方法无法访问任何实例成员变量 - 将其视为“您可以从任何地方调用的方法,而无需事先创建(新建)包含它的事物”
  • 和我说的有什么不同?
  • 除非使用非静态成员会产生另一个错误消息。 (需要对象引用)。​​
  • @Henk Holterman 在我的代码中,theGrid 只是一个局部变量,而不是一个字段
【解决方案2】:

确切的错误消息表明Tile 的可访问性低于public。
但是在您列出的Tile 中,它是公开的。

可能的原因

  • 其他类型之一,ResourceRule 被声明为内部类型(即没有 public
  • 你有另一个Tile
  • public class Tile 的发布代码不正确。
  • 错误信息引用不正确

【讨论】:

    猜你喜欢
    • 2018-03-07
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 2014-07-25
    • 2011-04-28
    • 2012-10-11
    • 2014-06-25
    • 1970-01-01
    相关资源
    最近更新 更多