2013-05-22 42 views
-4

我對無法impliciltly轉換類型 'System.Collections.Generic.List <BallSeparation.Ball>' 到 '布爾'

for (Ball ball; ballList) 

此錯誤的錯誤信息是

"Cannot impliciltly convert type 'System.Collections.Generic.List<BallSeparation.Ball>' to 'bool'" 

i可以知道如何解決它?

public partial class StartGame : Form 
{ 
    public List<Ball> ballList { get; private set; } 

    /**int bBA1; //The x axis from the upper left corner 
    int bBA2; //The y axis from the upper left corner 
    int spdBBA1; //The change of x 
    int spdBBA2; //The change of y 
    **/ 

    public StartGame() 
    { 
     this.ballList = new List<Ball>(); 
     InitializeComponent(); 
    } 

    private void StartGame_Load(object sender, EventArgs e) 
    { 
     ballList.add(new Ball(5, 10, 1, 1)); 
     /** 
     //Loads the ball on the screen at bottom of the window 
     bBA1 = this.ClientSize.Width/5; //The x axis the ball is loaded at 
     bBA2 = this.ClientSize.Height - 10; //The y axis the ball is loaded at 
     spdBBA1 = 1; //The speed of the ball of y 
     spdBBA2 = 1; //The speed of the ball of x 
     **/ 
    } 

    private void StartGame_Paint_1(object sender, PaintEventArgs e) 
    { 
     //This foreach loop will run through all the balls in ballList 
     for (Ball ball; ballList) 
     { 
      e.Graphics.FillEllipse(Brushes.Blue, ball.positionX, ball.positionY, 10, 10); 
     } 
     /** 
     //This is the inner paint color of the circle that is 10 by 10 
     e.Graphics.FillEllipse(Brushes.Blue, bBA1, bBA2, 10, 10); 
     //This is the outline paint color of the circle that is 10 by 10 
     e.Graphics.DrawEllipse(Pens.Blue, bBA1, bBA2, 10, 10); 
     **/ 
    } 

    private void timer1_Tick(object sender, EventArgs e) 
    { 
     /** 
     bBA2 = bBA2 + spdBBA2; 
     bBA1 = bBA1 + spdBBA1; 

     if (bBA2 < 0) 
     { 
      spdBBA2 = -spdBBA2; //If y is less than 0 then it changes direction 
     } 
     else if (bBA1 < -5) 
     { 
      spdBBA1 = -spdBBA1; 
     } 
     else if (bBA2 + 10 > this.ClientSize.Height) 
     { 
      spdBBA2 = -spdBBA2; //If y + 10, the radius of the circle is greater than the form width then we change direction 
     } 
     else if (bBA1 + 10 > this.ClientSize.Width) 
     { 
      spdBBA1 = -spdBBA1; 
     } 

     this.Invalidate(); 
     **/ 
    } 
+0

您在哪部分代碼中收到錯誤? – Sachin

+0

你可以縮小到給你麻煩的特定線路嗎? –

+0

這個(和你在這裏的其他問題)並不是碰巧是某種形式的作業/學習作業,對吧?好... – Onots

回答

1

C#不是Java。這是有點的Java:

for (Ball ball; ballList) 

正確的C#:

foreach (Ball ball in ballList) 

我注意到你正在編寫一個遊戲,使用定時器。您可能需要看看here,以解釋爲什麼傳統計時器通常不會在遊戲中使用。

編輯的後續評論:

表單是公開的。它有一個球列表的公共財產。現在你的球類很可能不公開。這是不一致的。你的表單不應該暴露未知類型。要麼讓球公開,要麼使球類型的方法和屬性暴露出來。

+1

[已修復最後一部分內容。](http://i.stack.imgur.com/WBov3.png) –

+0

謝謝。但是,現在我在公開列表 ballList {get;私人設置; }。它表示不一致的可訪問性。我可以知道我該如何解決它? – Guang

1

更改此:

for (Ball ball; ballList) 

這樣:

foreach (Ball ball in ballList) 
4

for (Ball ball; ballList)實際上不是foreach循環。這是一個for循環,以ballList作爲條件(因此被視爲一個bool)。什麼,你可能想要的是

foreach (Ball ball in ballList) 
+0

謝謝。但是,現在我在公開列表 ballList {get;私人設置; }。它表示不一致的可訪問性。我可以知道我該如何解決它? – Guang

+1

@光:這個網站上的搜索功能工作得很好。如果失敗了,您甚至可以使用Google。請參閱此問題以獲取答案:[不一致的可訪問性:屬性類型](http://stackoverflow.com/q/10322581)此外,MSDN文檔中還涉及此問題。不要無奈。 –

0
for (Ball ball; ballList) 

應該

foreach(Ball ball in ballList) 

一個for聲明的第二部分是測試條件(保持運行的循環);因此您試圖通過ballList作爲bool表達式。

2

你的問題是以下行:

for (Ball ball; ballList) 

for循環的語法是:

for (some assignment; some condition; some statement(s)) 

翻閱ballList像你想,一個foreach循環,而不是:

foreach (Ball ball in ballList) 
{ 
    // ... 
} 
相關問題