2011-11-23 111 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張貼的代碼不正確。
  • 錯誤消息引用不正確
相關問題