2011-11-23 115 views
0

我正在尝试为我的程序做一些简单的类,并且我被我的Tile [,]类'theGrid'对象的字段类型错误的不一致可访问性难倒了。我已经看过其他一些解决方案,并将所有内容设置为公开,但我仍然坚持如何处理这个问题。C#中字段类型的不一致可访问性

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

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

而这里的瓷砖类的简短版本:

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

我会很感激任何帮助,您可以给我这一个。

+0

你能告诉你的'Tile'类的属性/字段? – BoltClock

回答

2

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

您需要创建砖的新阵列

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

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

这个。一个静态方法不能访问任何实例成员变量 - 把它想象成“你可以从任何地方调用的方法,而无需事先创建(新的)包含它的东西” – JerKimball

+0

它与我所说的有什么不同? – alexm

+0

除了使用非静态成员会产生另一个错误消息。 (需要对象参考)。 –

1

确切的错误信息表明,Tile无障碍小于公众。
但在你列出的Tile它是公开的。

可能的原因

  • 其他类型之一,ResourceRule声明内部(即没有0​​)
  • 你有另一个Tile
  • public class Tile张贴的代码不正确。
  • 错误消息引用不正确
相关问题