【问题标题】:Cannot impliciltly convert type 'System.Collections.Generic.List<BallSeparation.Ball>' to 'bool' [closed]无法将类型“System.Collections.Generic.List<Ball Separation.Ball>”隐式转换为“bool”[关闭]
【发布时间】:2013-05-17 03:25:51
【问题描述】:

我有这个错误

for (Ball ball; ballList)

错误信息是

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

请问我该如何解决?

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();
        **/
    }

【问题讨论】:

  • 您在代码的哪一部分遇到了这个错误?
  • 你能把范围缩小到给你带来麻烦的特定行吗?
  • 这(以及您在这里的其他问题)不会碰巧是某种形式的家庭作业/学习作业,对吧?好的...
  • @Onots 我认为他是一个“Cookbook 程序员”并且并不真正知道他在 C# 中做什么:)
  • 这是我正在做的一个项目。

标签: c# .net winforms generics type-conversion


【解决方案1】:

for (Ball ball; ballList) 行实际上并不是一个 foreach 循环。这是一个 for 循环,以 ballList 作为条件(因此被视为布尔值)。你可能想要的是

 foreach (Ball ball in ballList)

【讨论】:

  • 谢谢。但是,现在我在 public List ballList { get; 上有另一个错误。私人套装; }。它声明了不一致的可访问性。请问我该如何解决?
  • @Guang:这个网站的搜索功能很好用。如果失败,您甚至可以使用 Google。请参阅此问题以获取答案:Inconsistent accessibility: property type 此外,MSDN 文档中涵盖了此问题。不要束手无策。
【解决方案2】:

您的问题是以下行:

for (Ball ball; ballList)

for 循环的语法是:

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

如您所愿地查看ballList,请使用foreach 循环:

foreach (Ball ball in ballList)
{
    // ...
}

【讨论】:

    【解决方案3】:

    C# 不是 Java。这有点像 Java:

    for (Ball ball; ballList)
    

    正确的 C#:

    foreach ( Ball ball in ballList)
    

    我注意到您正在编写游戏并使用计时器。您可能想看看here,了解为什么游戏中通常不使用传统计时器。

    为后续评论编辑:

    您的表单是公开的。它具有球列表的公共属性。现在你的 Ball 类很可能是不公开的。那是不一致的。您的表单不应公开未知类型。要么将 Ball 设为公开,要么将公开 Ball 类型的方法和属性设为私有。

    【讨论】:

    • 谢谢。但是,现在我在 public List ballList { get;私人套装; }。它声明了不一致的可访问性。请问我该如何解决?
    【解决方案4】:

    改变这个:

    for (Ball ball; ballList)
    

    到这里:

    foreach (Ball ball in ballList)
    

    【讨论】:

      【解决方案5】:
      for (Ball ball; ballList)
      

      应该是

      foreach(Ball ball in ballList)
      

      for 语句的第二部分是测试条件(保持循环运行);因此,您试图将ballList 作为bool 表达式传递。

      【讨论】:

        猜你喜欢
        • 2021-09-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多